結果

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

ソースコード

diff #
raw source code

#include <iostream>
#include <unordered_map>
#include <string>
using namespace std;

int main() {
    string S;
    cin >> S;

    unordered_map<int, int> firstOccur;
    int balance = 0;
    int maxLen = 0;

    // ?????balance=0 ????? -1??????
    firstOccur[0] = -1;

    for (int i = 0; i < S.length(); i++) {
        if (S[i] == 'A') {
            balance++;
        } else { // S[i] == 'B'
            balance--;
        }

        if (firstOccur.find(balance) != firstOccur.end()) {
            // ?????????? balance??????????
            maxLen = max(maxLen, i - firstOccur[balance]);
        } else {
            // ??????? balance?????
            firstOccur[balance] = i;
        }
    }

    cout << maxLen << endl;
    return 0;
}
0