結果

問題 No.672 最長AB列
ユーザー tentententen
提出日時 2020-09-04 09:05:06
言語 Java21
(openjdk 21)
結果
AC  
実行時間 304 ms / 2,000 ms
コード長 652 bytes
コンパイル時間 1,973 ms
コンパイル使用メモリ 71,832 KB
実行使用メモリ 72,976 KB
最終ジャッジ日時 2023-08-16 12:38:38
合計ジャッジ時間 7,127 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 113 ms
56,036 KB
testcase_01 AC 113 ms
56,084 KB
testcase_02 AC 115 ms
55,620 KB
testcase_03 AC 115 ms
55,460 KB
testcase_04 AC 115 ms
55,620 KB
testcase_05 AC 116 ms
55,640 KB
testcase_06 AC 115 ms
55,696 KB
testcase_07 AC 116 ms
55,972 KB
testcase_08 AC 115 ms
55,720 KB
testcase_09 AC 303 ms
72,976 KB
testcase_10 AC 304 ms
66,696 KB
testcase_11 AC 301 ms
67,204 KB
testcase_12 AC 261 ms
59,092 KB
testcase_13 AC 259 ms
59,172 KB
testcase_14 AC 266 ms
58,924 KB
testcase_15 AC 263 ms
58,736 KB
testcase_16 AC 262 ms
59,068 KB
testcase_17 AC 281 ms
65,360 KB
testcase_18 AC 238 ms
58,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
 	public static void main (String[] args) {
    	Scanner sc = new Scanner(System.in);
    	char[] arr = sc.next().toCharArray();
    	int current = 0;
    	int max = 0;
    	HashMap<Integer, Integer> places = new HashMap<>();
    	places.put(0, -1);
    	for (int i = 0; i < arr.length; i++) {
    	    if (arr[i] == 'A') {
    	        current++;
    	    } else {
    	        current--;
    	    }
    	    if (places.containsKey(current)) {
    	        max = Math.max(max, i - places.get(current));
    	    } else {
    	        places.put(current, i);
    	    }
    	}
    	System.out.println(max);
	}
}
0