結果

問題 No.672 最長AB列
ユーザー silviasetitechsilviasetitech
提出日時 2020-03-15 15:43:07
言語 Java21
(openjdk 21)
結果
AC  
実行時間 208 ms / 2,000 ms
コード長 1,474 bytes
コンパイル時間 2,297 ms
コンパイル使用メモリ 77,564 KB
実行使用メモリ 56,808 KB
最終ジャッジ日時 2024-05-03 21:31:41
合計ジャッジ時間 5,926 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 96 ms
52,744 KB
testcase_01 AC 113 ms
54,124 KB
testcase_02 AC 113 ms
54,056 KB
testcase_03 AC 98 ms
53,056 KB
testcase_04 AC 113 ms
53,944 KB
testcase_05 AC 115 ms
54,084 KB
testcase_06 AC 106 ms
53,128 KB
testcase_07 AC 101 ms
53,024 KB
testcase_08 AC 98 ms
53,292 KB
testcase_09 AC 205 ms
56,808 KB
testcase_10 AC 199 ms
56,592 KB
testcase_11 AC 192 ms
56,540 KB
testcase_12 AC 200 ms
56,736 KB
testcase_13 AC 205 ms
56,520 KB
testcase_14 AC 198 ms
56,472 KB
testcase_15 AC 191 ms
56,664 KB
testcase_16 AC 208 ms
56,524 KB
testcase_17 AC 184 ms
56,584 KB
testcase_18 AC 184 ms
56,712 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author silviase
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        longestABSubString solver = new longestABSubString();
        solver.solve(1, in, out);
        out.close();
    }

    static class longestABSubString {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            String s = in.next();
            int l = s.length();
            int[] dif = new int[l * 2 + 2];
            int now = l;
            Arrays.fill(dif, -1);

            dif[now] = 0;
            int res = 0;
            for (int i = 0; i < l; i++) {
                if (s.charAt(i) == 'A') {
                    now++;
                } else {
                    now--;
                }

                if (dif[now] == -1) {
                    // 更新する
                    dif[now] = i + 1;
                } else {
                    // 比較する
                    res = Math.max(res, i + 1 - dif[now]);
                }
            }
            out.println(res);
        }

    }
}

0