結果

問題 No.672 最長AB列
ユーザー tentententen
提出日時 2023-08-01 10:48:12
言語 Java21
(openjdk 21)
結果
AC  
実行時間 181 ms / 2,000 ms
コード長 1,989 bytes
コンパイル時間 2,757 ms
コンパイル使用メモリ 90,328 KB
実行使用メモリ 57,680 KB
最終ジャッジ日時 2024-04-19 10:21:33
合計ジャッジ時間 5,237 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
37,040 KB
testcase_01 AC 47 ms
37,180 KB
testcase_02 AC 46 ms
37,340 KB
testcase_03 AC 48 ms
37,320 KB
testcase_04 AC 47 ms
37,132 KB
testcase_05 AC 45 ms
37,120 KB
testcase_06 AC 45 ms
37,392 KB
testcase_07 AC 44 ms
37,124 KB
testcase_08 AC 46 ms
37,264 KB
testcase_09 AC 181 ms
57,680 KB
testcase_10 AC 169 ms
53,088 KB
testcase_11 AC 176 ms
51,720 KB
testcase_12 AC 155 ms
43,488 KB
testcase_13 AC 160 ms
44,480 KB
testcase_14 AC 157 ms
44,708 KB
testcase_15 AC 146 ms
44,368 KB
testcase_16 AC 144 ms
43,604 KB
testcase_17 AC 175 ms
50,900 KB
testcase_18 AC 140 ms
40,128 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        char[] inputs = sc.next().toCharArray();
        HashMap<Integer, Integer> prev = new HashMap<>();
        prev.put(0, -1);
        int current = 0;
        int ans = 0;
        for (int i = 0; i < inputs.length; i++) {
            if (inputs[i] == 'A') {
                current++;
            } else {
                current--;
            }
            if (prev.containsKey(current)) {
                ans = Math.max(ans, i - prev.get(current));
            } else {
                prev.put(current, i);
            }
        }
        System.out.println(ans);
    }
} 
class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
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 int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0