結果

問題 No.672 最長AB列
ユーザー CELICACELICA
提出日時 2020-08-18 20:56:18
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 5 ms / 2,000 ms
コード長 621 bytes
コンパイル時間 1,316 ms
コンパイル使用メモリ 165,284 KB
実行使用メモリ 6,784 KB
最終ジャッジ日時 2024-04-20 07:55:52
合計ジャッジ時間 2,200 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 0 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 4 ms
6,656 KB
testcase_10 AC 4 ms
6,656 KB
testcase_11 AC 4 ms
6,528 KB
testcase_12 AC 4 ms
6,656 KB
testcase_13 AC 5 ms
6,784 KB
testcase_14 AC 5 ms
6,656 KB
testcase_15 AC 5 ms
6,528 KB
testcase_16 AC 4 ms
6,784 KB
testcase_17 AC 5 ms
6,784 KB
testcase_18 AC 4 ms
6,656 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ul = unsigned long;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    string s;
    cin >> s;

    long n = s.size();

    if (n <= 1)
    {
        cout << "0\n";
        return 0;
    }

    long sum{ n };
    vector<long> vidx(n * 2 + 1, n);
    vidx[sum] = -1;

    long res{ 0 };
    for (long i = 0; i < n; ++i)
    {
        s[i] == 'A' ? ++sum : --sum;

        if (vidx[sum] == n)
            vidx[sum] = i;
        else
            res = max(res, i - vidx[sum]);
    }

    cout << res << "\n";

    return 0;
}
0