結果

問題 No.672 最長AB列
ユーザー ama_nukoama_nuko
提出日時 2018-06-08 17:08:57
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 152 ms / 2,000 ms
コード長 1,091 bytes
コンパイル時間 826 ms
コンパイル使用メモリ 92,800 KB
実行使用メモリ 26,692 KB
最終ジャッジ日時 2023-09-12 23:20:38
合計ジャッジ時間 2,499 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 152 ms
26,692 KB
testcase_10 AC 98 ms
15,872 KB
testcase_11 AC 92 ms
15,848 KB
testcase_12 AC 20 ms
7,360 KB
testcase_13 AC 20 ms
7,660 KB
testcase_14 AC 20 ms
7,156 KB
testcase_15 AC 21 ms
7,508 KB
testcase_16 AC 20 ms
7,160 KB
testcase_17 AC 98 ms
15,816 KB
testcase_18 AC 14 ms
7,484 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cmath>
#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <utility>
#include <iomanip>

#define int long long int
#define rep(i, n) for(int i = 0; i < (n); ++i)

using namespace std;

typedef pair<int, int> P;

const int INF = 1e15;
const int MOD = 1e9+7;

signed main(){
    string s;
    cin >> s;

    vector<int> diff(s.length()+1);

    for(int i = 0; i < (int)s.length(); i++){
        if(s[i] == 'A'){
            diff[i+1] = diff[i] + 1;
        }
        if(s[i] == 'B'){
            diff[i+1] = diff[i] - 1;
        }
    }

    map<int, vector<int>> m;

    rep(i, (int)s.length()+1){
        if(m.find(diff[i]) == m.end()){
            m.emplace(diff[i], vector<int>());
        }
        m[diff[i]].push_back(i);
    }

    int ans = 0;
    for(auto it = m.begin(); it != m.end(); it++){
        vector<int> v = (*it).second;
        sort(v.begin(), v.end());
        int tmp = v[v.size()-1] - v[0];
        if(tmp > ans){
            ans = tmp;
        }
    }
    cout << ans << endl;

    return 0;
}
0