結果

問題 No.672 最長AB列
ユーザー takeya_okinotakeya_okino
提出日時 2018-05-05 20:47:22
言語 Java21
(openjdk 21)
結果
AC  
実行時間 330 ms / 2,000 ms
コード長 551 bytes
コンパイル時間 2,085 ms
コンパイル使用メモリ 77,300 KB
実行使用メモリ 72,188 KB
最終ジャッジ日時 2023-09-10 10:39:06
合計ジャッジ時間 7,955 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 115 ms
55,628 KB
testcase_01 AC 116 ms
55,736 KB
testcase_02 AC 116 ms
55,996 KB
testcase_03 AC 116 ms
55,536 KB
testcase_04 AC 115 ms
55,656 KB
testcase_05 AC 115 ms
56,016 KB
testcase_06 AC 116 ms
55,652 KB
testcase_07 AC 116 ms
55,648 KB
testcase_08 AC 117 ms
55,564 KB
testcase_09 AC 330 ms
72,188 KB
testcase_10 AC 328 ms
67,248 KB
testcase_11 AC 284 ms
65,544 KB
testcase_12 AC 259 ms
59,060 KB
testcase_13 AC 263 ms
58,800 KB
testcase_14 AC 274 ms
58,708 KB
testcase_15 AC 265 ms
59,104 KB
testcase_16 AC 264 ms
59,672 KB
testcase_17 AC 295 ms
65,340 KB
testcase_18 AC 258 ms
58,872 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

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