結果

問題 No.672 最長AB列
ユーザー vintersn0w
提出日時 2018-05-08 15:58:19
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 216 ms / 2,000 ms
コード長 776 bytes
コンパイル時間 955 ms
コンパイル使用メモリ 79,700 KB
実行使用メモリ 22,400 KB
最終ジャッジ日時 2024-06-28 02:27:21
合計ジャッジ時間 2,144 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <stdio.h>
#include <cmath>
#include <map>

using namespace std;
typedef uint uint32_t;


int main() {
    string S;
    cin >> S;
    int N = S.length();

    map<int, int> mr, ml;

    int c = 0;

    ml[0] = -1;
    for (int i = 0; i < N; i++) {
        if (S[i] == 'A') c++;
        else c--;

        if (ml.count(c) == 0) {
            ml[c] = i;
        }
        mr[c] = i;
    }

    int m = 0;
    for (auto itr = ml.begin(); itr != ml.end(); itr++) {
        if (mr.count(itr->first) == 0) continue;
        int rp = mr[itr->first];
        int lp = itr->second;
        // cout << itr->first << ' ' << itr->second << ' ' << rp << endl;
        m = max(m, rp - lp);
    }

    cout << m << endl;
}
0