結果

問題 No.672 最長AB列
コンテスト
ユーザー vjudge1
提出日時 2026-01-16 14:35:22
言語 C++14
(gcc 15.2.0 + boost 1.89.0)
結果
AC  
実行時間 8 ms / 2,000 ms
コード長 706 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,760 ms
コンパイル使用メモリ 184,344 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2026-01-16 14:35:25
合計ジャッジ時間 2,930 ms
ジャッジサーバーID
(参考情報)
judge6 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;
const int OFFSET = 200005;
const int MAX_RANGE = 400010;
int pos[MAX_RANGE];
int main() {
    string s;
    if (!(cin >> s)) return 0;
    int n = s.length();
    memset(pos, -1, sizeof(pos));
    pos[0 + OFFSET] = 0;
    int current_sum = 0;
    int max_len = 0;
    for (int i = 1; i <= n; ++i) {
        int val = (s[i-1] == 'A' ? 1 : -1);
        current_sum += val;
        int idx = current_sum + OFFSET;
        if (pos[idx] != -1) {
            int len = i - pos[idx];
            if (len > max_len) {
                max_len = len;
            }
        } else {
            pos[idx] = i;
        }
    }
    cout << max_len << endl;

    return 0;
}
0