結果

問題 No.672 最長AB列
ユーザー CELICACELICA
提出日時 2020-08-17 21:57:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 90 ms / 2,000 ms
コード長 699 bytes
コンパイル時間 1,484 ms
コンパイル使用メモリ 170,272 KB
実行使用メモリ 17,408 KB
最終ジャッジ日時 2024-04-19 19:10:14
合計ジャッジ時間 2,776 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 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 1 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 1 ms
5,376 KB
testcase_09 AC 90 ms
17,408 KB
testcase_10 AC 66 ms
11,136 KB
testcase_11 AC 58 ms
11,264 KB
testcase_12 AC 10 ms
5,376 KB
testcase_13 AC 10 ms
5,376 KB
testcase_14 AC 9 ms
5,376 KB
testcase_15 AC 9 ms
5,376 KB
testcase_16 AC 9 ms
5,376 KB
testcase_17 AC 62 ms
11,264 KB
testcase_18 AC 4 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

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

    string s;
    cin >> s;

    long n = (long)s.size();
    if (n <= 1)
    {
        cout << "0\n";
        return 0;
    }

    vector<long> vsum(n + 1, 0);

    for (long i = 0; i < n; ++i)
        vsum[i + 1] = s[i] == 'A' ? 1 : -1;

    for (long i = 1; i < n + 1; ++i)
        vsum[i] += vsum[i - 1];

    long res{ 0 };
    map<long, long> m;

    for (long i = 0; i < n + 1; ++i)
    {
        if (!m.count(vsum[i]))
            m[vsum[i]] = i;
        else
            res = max(res, i - m[vsum[i]]);
    }

    cout << res << "\n";

    return 0;
}
0