結果

問題 No.672 最長AB列
ユーザー takeya_okino
提出日時 2018-05-05 20:47:22
言語 Java
(openjdk 23)
結果
AC  
実行時間 332 ms / 2,000 ms
コード長 551 bytes
コンパイル時間 2,133 ms
コンパイル使用メモリ 74,432 KB
実行使用メモリ 59,092 KB
最終ジャッジ日時 2024-06-28 02:02:31
合計ジャッジ時間 7,200 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

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