結果
問題 | No.672 最長AB列 |
ユーザー |
![]() |
提出日時 | 2018-10-01 14:47:20 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 388 ms / 2,000 ms |
コード長 | 801 bytes |
コンパイル時間 | 2,180 ms |
コンパイル使用メモリ | 78,024 KB |
実行使用メモリ | 77,072 KB |
最終ジャッジ日時 | 2024-10-12 09:46:33 |
合計ジャッジ時間 | 7,212 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 16 |
ソースコード
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);}}