結果

問題 No.672 最長AB列
ユーザー tentententen
提出日時 2023-04-14 14:40:46
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,231 bytes
コンパイル時間 3,119 ms
コンパイル使用メモリ 92,364 KB
実行使用メモリ 76,156 KB
最終ジャッジ日時 2024-04-18 13:13:31
合計ジャッジ時間 7,568 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
37,228 KB
testcase_01 AC 52 ms
37,324 KB
testcase_02 AC 53 ms
37,176 KB
testcase_03 AC 52 ms
37,248 KB
testcase_04 AC 54 ms
37,180 KB
testcase_05 AC 52 ms
37,180 KB
testcase_06 AC 53 ms
36,948 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 384 ms
76,156 KB
testcase_10 AC 314 ms
66,076 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 296 ms
63,188 KB
testcase_18 WA -
権限があれば一括ダウンロードができます

ソースコード

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, ArrayList<Integer>> places = new HashMap<>();
        places.put(0, new ArrayList<>());
        places.get(0).add(-1);
        int current = 0;
        for (int i = 0; i < inputs.length; i++) {
            if (inputs[i] == 'A') {
                current++;
            } else {
                current--;
            }
            if (!places.containsKey(current)) {
                places.put(current, new ArrayList<>());
            }
            places.get(current).add(i);
        }
        int ans = 0;
        for (ArrayList<Integer> list : places.values()) {
            for (int i = 1; i < list.size(); i++) {
                ans = Math.max(ans, list.get(i) - list.get(i - 1));
            }
        }
        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