結果

問題 No.672 最長AB列
ユーザー YamaKasaYamaKasa
提出日時 2018-10-01 14:47:20
言語 Java21
(openjdk 21)
結果
AC  
実行時間 396 ms / 2,000 ms
コード長 801 bytes
コンパイル時間 2,102 ms
コンパイル使用メモリ 74,480 KB
実行使用メモリ 79,140 KB
最終ジャッジ日時 2023-08-02 14:10:16
合計ジャッジ時間 7,379 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 117 ms
55,604 KB
testcase_01 AC 118 ms
56,204 KB
testcase_02 AC 117 ms
56,008 KB
testcase_03 AC 115 ms
55,704 KB
testcase_04 AC 116 ms
55,912 KB
testcase_05 AC 116 ms
56,172 KB
testcase_06 AC 117 ms
56,000 KB
testcase_07 AC 117 ms
56,156 KB
testcase_08 AC 118 ms
56,200 KB
testcase_09 AC 376 ms
79,140 KB
testcase_10 AC 396 ms
79,112 KB
testcase_11 AC 380 ms
78,584 KB
testcase_12 AC 284 ms
59,712 KB
testcase_13 AC 289 ms
59,060 KB
testcase_14 AC 285 ms
59,072 KB
testcase_15 AC 285 ms
58,800 KB
testcase_16 AC 279 ms
59,432 KB
testcase_17 AC 390 ms
78,376 KB
testcase_18 AC 268 ms
60,220 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		String S = scan.next();
		scan.close();
		int len = S.length();
		Map<Integer, Integer> mapL = new HashMap<Integer, Integer>();
		Map<Integer, Integer> mapR = new HashMap<Integer, Integer>();
		int k = 0;
		mapL.put(0, 0);
		for(int i = 0; i < len; i++) {
			char c = S.charAt(i);
			if(c == 'A') {
				k++;
			}else {
				k--;
			}
			if(!mapL.containsKey(k)) {
				mapL.put(k, i + 1);
			}else {
				mapR.put(k, i + 1);
			}
		}
		int max = 0;
		for(int key : mapL.keySet()) {
			if(mapR.containsKey(key)) {
				int t = mapR.get(key) - mapL.get(key);
				max = Math.max(max, t);
			}
		}
		System.out.println(max);
	}
}
0