結果

問題 No.672 最長AB列
ユーザー vintersn0wvintersn0w
提出日時 2018-05-08 15:58:19
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 216 ms / 2,000 ms
コード長 776 bytes
コンパイル時間 583 ms
コンパイル使用メモリ 79,064 KB
実行使用メモリ 22,200 KB
最終ジャッジ日時 2023-09-10 11:04:20
合計ジャッジ時間 2,136 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 216 ms
22,200 KB
testcase_10 AC 129 ms
12,704 KB
testcase_11 AC 119 ms
12,808 KB
testcase_12 AC 14 ms
4,380 KB
testcase_13 AC 14 ms
4,380 KB
testcase_14 AC 14 ms
4,376 KB
testcase_15 AC 14 ms
4,380 KB
testcase_16 AC 14 ms
4,380 KB
testcase_17 AC 122 ms
12,760 KB
testcase_18 AC 6 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <stdio.h>
#include <cmath>
#include <map>

using namespace std;
typedef uint uint32_t;


int main() {
    string S;
    cin >> S;
    int N = S.length();

    map<int, int> mr, ml;

    int c = 0;

    ml[0] = -1;
    for (int i = 0; i < N; i++) {
        if (S[i] == 'A') c++;
        else c--;

        if (ml.count(c) == 0) {
            ml[c] = i;
        }
        mr[c] = i;
    }

    int m = 0;
    for (auto itr = ml.begin(); itr != ml.end(); itr++) {
        if (mr.count(itr->first) == 0) continue;
        int rp = mr[itr->first];
        int lp = itr->second;
        // cout << itr->first << ' ' << itr->second << ' ' << rp << endl;
        m = max(m, rp - lp);
    }

    cout << m << endl;
}
0