結果

問題 No.672 最長AB列
ユーザー @abcde@abcde
提出日時 2019-03-16 03:33:07
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,403 bytes
コンパイル時間 2,023 ms
コンパイル使用メモリ 144,708 KB
実行使用メモリ 36,460 KB
最終ジャッジ日時 2023-09-14 15:01:27
合計ジャッジ時間 2,640 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
34,576 KB
testcase_01 AC 18 ms
34,660 KB
testcase_02 AC 19 ms
34,632 KB
testcase_03 AC 18 ms
34,660 KB
testcase_04 WA -
testcase_05 AC 18 ms
34,652 KB
testcase_06 AC 18 ms
34,572 KB
testcase_07 AC 19 ms
34,604 KB
testcase_08 AC 19 ms
34,604 KB
testcase_09 AC 25 ms
36,232 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 25 ms
36,336 KB
testcase_14 WA -
testcase_15 AC 25 ms
36,228 KB
testcase_16 AC 26 ms
36,460 KB
testcase_17 AC 25 ms
36,352 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', 'B' の 出現数 の 累積和は?
    int len = S.size();
    int A[len], B[len];
    for(int i = 0; i < len; i++) A[i] = 0, B[i] = 0;
    if(S[0] == 'A') A[0]++;
    if(S[0] == 'B') B[0]++;
    for(int i = 1; i < len; i++){
        A[i] = A[i - 1], B[i] = B[i - 1];
        if(S[i] == 'A') A[i]++;
        if(S[i] == 'B') B[i]++;
    }
    
    // 3. 'A' なら 1 加算, 'B' なら 1 減算(解説通り).
    // -> サイズ 4 * 1e6 + 1 の配列を用意し, index へ埋め込んでみる.
    int LIMIT = 4 * 1e6 + 1;
    int upper[LIMIT], lower[LIMIT];
    for(int i = 0; i < LIMIT; i++) upper[i] = 0, lower[i] = LIMIT;
    for(int i = 0; i < len; i++){
        // マイナス防止のため, 2 * 1e6 を 加算.
        int diff = A[i] - B[i] + (2 * 1e6);
        if(i > upper[diff]) upper[diff] = i;
        if(i < lower[diff]) lower[diff] = i;
    }
    
    // 4. 後処理.
    // ex.
    // BBABBAABABAAABBABAAABAAABBAAABAAABBABBBB -> 20 (OK のはず).
    int ans = 0;
    for(int i = 0; i < LIMIT; i++) ans = max(ans, upper[i] - lower[i]);
    cout << ans << endl;
    return 0;
    
}
0