結果

問題 No.672 最長AB列
ユーザー @abcde@abcde
提出日時 2019-03-18 21:53:06
言語 C++11
(gcc 11.4.0)
結果
RE  
実行時間 -
コード長 1,650 bytes
コンパイル時間 1,570 ms
コンパイル使用メモリ 149,512 KB
実行使用メモリ 19,576 KB
最終ジャッジ日時 2023-09-26 10:31:22
合計ジャッジ時間 4,308 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 RE -
testcase_04 WA -
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 25 ms
19,576 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 13 ms
14,568 KB
testcase_14 WA -
testcase_15 AC 12 ms
14,904 KB
testcase_16 AC 13 ms
14,556 KB
testcase_17 AC 20 ms
16,452 KB
testcase_18 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

// 解答不能と判断したので, 下記, 解説を確認後, 再提出.
// https://yukicoder.me/problems/no/672/editorial
#include <bits/stdc++.h>
using namespace std;
const int LIMIT = 2 * 1e6;

int main() {
    
    // 1. 入力情報取得.
    string S;
    cin >> S;
    
    // 2. 'A' なら 1 加算, 'B' なら 1 減算(解説通り).
    int l = S.size();
    int a[l];
    a[0] = (S[0] == 'A') ? l + 1 : l - 1;
    for(int i = 1; i < l; i++){
        if(S[i] == 'A') a[i] = a[i - 1] + 1;
        if(S[i] == 'B') a[i] = a[i - 1] - 1;
    }
    // for(int i = 0; i < l; i++) cout << "a[" << i << "]=" << a[i] << endl;
    
    // 3. 各 a[i] の 情報 を, vector に保存.
    vector<vector<int>> vi(2 * l, vector<int>(0));
    for(int i = 0; i < l; i++) vi[a[i]].push_back(i);
    
    // 4. 集計.
    int ans = 0;
    // for(int i = 0; i < l * 2; i++){
    //     if(!vi[i].empty()){
    //         cout << "i=" << i << " p=";
    //         for(auto &p : vi[i]) cout << p << " ";
    //         cout << endl;
    //     }
    // }
    // ex.
    // BBABBAABABAAABBABAAABAAABBAAABAAABBABBBB
    // i=37 p=4 
    // i=38 p=1 3 5 7 9 
    // i=39 p=0 2 6 8 10 14 16 
    // i=40 p=11 13 15 17 
    // i=41 p=12 18 20 
    // i=42 p=19 21 25 39 
    // i=43 p=22 24 26 38 
    // i=44 p=23 27 29 37 
    // i=45 p=28 30 34 36 
    // i=46 p=31 33 35 
    // i=47 p=32 
    // -> i = 42 で, 39 - 19 = 20(OK のはず) の 最大値 を 取ってこれるはず.
    for(int i = 0; i < l * 2; i++) if(!vi[i].empty()) ans = max(ans, vi[i].back() - vi[i].front());
    
    // 5. 後処理.
    cout << ans << endl;
    return 0;
    
}
0