結果

問題 No.672 最長AB列
ユーザー face4
提出日時 2018-05-14 17:30:53
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 88 ms / 2,000 ms
コード長 540 bytes
コンパイル時間 1,021 ms
コンパイル使用メモリ 78,400 KB
実行使用メモリ 12,800 KB
最終ジャッジ日時 2024-06-28 11:46:14
合計ジャッジ時間 1,898 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<map>
using namespace std;

int main(){
    string s;
    cin >> s;
    map<int, int> left;
    left[0] = -1; // index0の位置から最長AB列が始まる場合のため
    
    int result = 0;
    int score = 0;
    for(int i = 0; i < s.length(); i++){
        if(s[i] == 'A')         score++;
        else if(s[i] == 'B')    score--;

        if(left.count(score))   result = max(result, i-left[score]);
        else                    left[score] = i;   
    }

    cout << result << endl;
    return 0;
}
0