結果

問題 No.672 最長AB列
ユーザー ilplrrilplrr
提出日時 2018-04-14 09:05:49
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 1,618 ms / 2,000 ms
コード長 832 bytes
コンパイル時間 1,300 ms
コンパイル使用メモリ 145,308 KB
実行使用メモリ 8,252 KB
最終ジャッジ日時 2023-09-10 04:25:06
合計ジャッジ時間 18,613 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
8,032 KB
testcase_01 AC 6 ms
7,952 KB
testcase_02 AC 6 ms
8,064 KB
testcase_03 AC 6 ms
8,040 KB
testcase_04 AC 6 ms
7,932 KB
testcase_05 AC 6 ms
8,244 KB
testcase_06 AC 6 ms
8,032 KB
testcase_07 AC 6 ms
8,000 KB
testcase_08 AC 6 ms
8,056 KB
testcase_09 AC 1,598 ms
8,124 KB
testcase_10 AC 1,583 ms
8,224 KB
testcase_11 AC 1,596 ms
8,220 KB
testcase_12 AC 1,582 ms
8,060 KB
testcase_13 AC 1,598 ms
8,076 KB
testcase_14 AC 1,611 ms
8,252 KB
testcase_15 AC 1,597 ms
8,224 KB
testcase_16 AC 1,589 ms
8,080 KB
testcase_17 AC 1,618 ms
8,236 KB
testcase_18 AC 1,584 ms
8,184 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);
    }

    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