結果

問題 No.672 最長AB列
ユーザー tentententen
提出日時 2023-04-14 14:43:03
言語 Java21
(openjdk 21)
結果
AC  
実行時間 363 ms / 2,000 ms
コード長 2,226 bytes
コンパイル時間 2,844 ms
コンパイル使用メモリ 92,368 KB
実行使用メモリ 85,504 KB
最終ジャッジ日時 2024-04-18 13:15:18
合計ジャッジ時間 6,240 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 50 ms
50,412 KB
testcase_01 AC 52 ms
50,320 KB
testcase_02 AC 50 ms
50,208 KB
testcase_03 AC 51 ms
50,328 KB
testcase_04 AC 50 ms
49,868 KB
testcase_05 AC 51 ms
50,396 KB
testcase_06 AC 52 ms
50,184 KB
testcase_07 AC 51 ms
50,276 KB
testcase_08 AC 51 ms
50,592 KB
testcase_09 AC 363 ms
85,504 KB
testcase_10 AC 261 ms
79,176 KB
testcase_11 AC 264 ms
77,148 KB
testcase_12 AC 179 ms
58,600 KB
testcase_13 AC 187 ms
60,332 KB
testcase_14 AC 172 ms
59,524 KB
testcase_15 AC 167 ms
57,648 KB
testcase_16 AC 178 ms
58,348 KB
testcase_17 AC 280 ms
75,400 KB
testcase_18 AC 152 ms
57,560 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, 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()) {
            if (list.size() >= 2) {
                ans = Math .max(ans, list.get(list.size() - 1) - list.get(0));
            }
        }
        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