結果

問題 No.672 最長AB列
ユーザー silviasetitechsilviasetitech
提出日時 2020-03-15 15:43:07
言語 Java21
(openjdk 21)
結果
AC  
実行時間 229 ms / 2,000 ms
コード長 1,474 bytes
コンパイル時間 1,958 ms
コンパイル使用メモリ 74,052 KB
実行使用メモリ 58,924 KB
最終ジャッジ日時 2023-08-16 13:07:28
合計ジャッジ時間 6,764 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 116 ms
55,536 KB
testcase_01 AC 116 ms
55,556 KB
testcase_02 AC 117 ms
55,464 KB
testcase_03 AC 116 ms
55,380 KB
testcase_04 AC 118 ms
55,972 KB
testcase_05 AC 116 ms
55,908 KB
testcase_06 AC 117 ms
55,824 KB
testcase_07 AC 117 ms
56,368 KB
testcase_08 AC 118 ms
55,972 KB
testcase_09 AC 218 ms
58,632 KB
testcase_10 AC 220 ms
58,684 KB
testcase_11 AC 222 ms
58,924 KB
testcase_12 AC 229 ms
58,404 KB
testcase_13 AC 216 ms
58,736 KB
testcase_14 AC 225 ms
58,488 KB
testcase_15 AC 219 ms
58,496 KB
testcase_16 AC 226 ms
56,572 KB
testcase_17 AC 220 ms
58,724 KB
testcase_18 AC 223 ms
58,508 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