結果

問題 No.672 最長AB列
ユーザー tentententen
提出日時 2022-10-06 13:29:23
言語 Java21
(openjdk 21)
結果
AC  
実行時間 220 ms / 2,000 ms
コード長 1,486 bytes
コンパイル時間 4,705 ms
コンパイル使用メモリ 74,876 KB
実行使用メモリ 70,232 KB
最終ジャッジ日時 2023-08-31 00:00:31
合計ジャッジ時間 7,338 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
49,404 KB
testcase_01 AC 42 ms
49,212 KB
testcase_02 AC 43 ms
49,528 KB
testcase_03 AC 43 ms
49,768 KB
testcase_04 AC 43 ms
49,752 KB
testcase_05 AC 43 ms
49,456 KB
testcase_06 AC 43 ms
49,184 KB
testcase_07 AC 43 ms
49,248 KB
testcase_08 AC 42 ms
49,336 KB
testcase_09 AC 220 ms
70,232 KB
testcase_10 AC 188 ms
64,644 KB
testcase_11 AC 191 ms
62,224 KB
testcase_12 AC 162 ms
54,536 KB
testcase_13 AC 166 ms
56,124 KB
testcase_14 AC 159 ms
55,816 KB
testcase_15 AC 156 ms
55,820 KB
testcase_16 AC 159 ms
55,904 KB
testcase_17 AC 188 ms
62,528 KB
testcase_18 AC 148 ms
53,568 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        char[] values = sc.next().toCharArray();
        HashMap<Integer, Integer> places = new HashMap<>();
        places.put(0, -1);
        int current = 0;
        int max = 0;
        for (int i = 0; i < values.length; i++) {
            if (values[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);
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0