結果

問題 No.672 最長AB列
ユーザー ilplrr
提出日時 2018-04-14 09:05:49
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 1,633 ms / 2,000 ms
コード長 832 bytes
コンパイル時間 1,277 ms
コンパイル使用メモリ 159,320 KB
実行使用メモリ 8,448 KB
最終ジャッジ日時 2024-06-27 20:07:01
合計ジャッジ時間 18,693 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int mi[500010];
int ma[500010];

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

    int a[200010] = {};
    for (int& x : mi) x = 1 << 29;
    for (int& x : ma) x = -(1 << 29);

    mi[200010] = 0;
    ma[200010] = 0;

    for (int i = 0; i < s.size(); i++) {
        if (i) a[i] = a[i -  1];
        if (s[i] == 'A') a[i]++;
        else a[i]--;
        mi[a[i] + 200010] = min(mi[a[i] + 200010], i + 1);
        ma[a[i] + 200010] = max(ma[a[i] + 200010], i + 1);
    }

    for (int i = 0; i < s.size(); i++) {
        cerr << a[i] << " " << mi[a[i] + 200010] << " " << ma[a[i] + 200010]  << endl;
    }

    int ans = 0;
    for (int i = 0; i < 500010; i++) {
        if (ma[i] == 1 << 29) continue;
        ans = max(ans, ma[i] - mi[i]);
    }
    cout << ans << endl;

    return 0;
}
0