結果

問題 No.672 最長AB列
ユーザー ilplrrilplrr
提出日時 2018-04-14 09:06:39
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 11 ms / 2,000 ms
コード長 697 bytes
コンパイル時間 1,560 ms
コンパイル使用メモリ 144,672 KB
実行使用メモリ 7,636 KB
最終ジャッジ日時 2023-09-10 04:23:07
合計ジャッジ時間 2,616 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
7,276 KB
testcase_01 AC 6 ms
7,220 KB
testcase_02 AC 5 ms
7,424 KB
testcase_03 AC 6 ms
7,216 KB
testcase_04 AC 6 ms
7,312 KB
testcase_05 AC 5 ms
7,508 KB
testcase_06 AC 6 ms
7,308 KB
testcase_07 AC 5 ms
7,220 KB
testcase_08 AC 6 ms
7,260 KB
testcase_09 AC 10 ms
7,504 KB
testcase_10 AC 10 ms
7,556 KB
testcase_11 AC 9 ms
7,424 KB
testcase_12 AC 11 ms
7,420 KB
testcase_13 AC 10 ms
7,636 KB
testcase_14 AC 10 ms
7,392 KB
testcase_15 AC 10 ms
7,436 KB
testcase_16 AC 11 ms
7,580 KB
testcase_17 AC 10 ms
7,444 KB
testcase_18 AC 10 ms
7,440 KB
権限があれば一括ダウンロードができます

ソースコード

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);
    }

    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