結果

問題 No.3395 Range Flipping Game
コンテスト
ユーザー nonon
提出日時 2025-12-02 10:58:12
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 33 ms / 2,000 ms
コード長 2,062 bytes
コンパイル時間 3,222 ms
コンパイル使用メモリ 283,852 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2025-12-02 10:58:18
合計ジャッジ時間 5,613 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;

using ll = long long;

bool chmin(auto &a, auto b) { return a > b ? a = b, true : false; }
bool chmax(auto &a, auto b) { return a < b ? a = b, true : false; }

template<typename T>
vector<pair<T, int>> run_length_encoding(const vector<T> &v) {
    vector<pair<T, int>> res;
    for (const auto &x : v) {
        if (res.empty() || res.back().first != x) {
            res.emplace_back(x, 0);
        }
        res.back().second++;
    }
    return res;
}

vector<pair<char, int>> run_length_encoding(const string &s) {
    return run_length_encoding(vector<char>(s.begin(), s.end()));
}

void solve() {
    int N;
    string S;
    cin >> N >> S;
    auto flip = [&] (int l, int r) -> void {
        for (int i = l; i < r; i++) S[i] ^= 'A' ^ 'B';
    };
    if (N <= 2) {
        cout << string(N, 'B') << '\n';
        return;
    }
    auto P = run_length_encoding(S);
    if (P.size() == 1) {
        if (P[0].first == 'A') {
            cout << "BB" + string(N - 2, 'A') << '\n';
        } else {
            cout << string(N, 'B') << '\n';
        }
        return;
    }
    if (P[0].first == 'A') {
        if (P[0].second == 1) {
            if (P[1].second == 1) {
                if (P.size() >= 4) {
                    int l = P[0].second + P[1].second + P[2].second;
                    int r = l + P[3].second;
                    flip(l, r);
                }
            } else {
                flip(2, P[0].second + P[1].second);
            }
        } else if (P[0].second == 2) {
            flip(1, P[0].second + P[1].second);
        } else {
            flip(1, 2);
        }
        flip(0, 1);
    } else {
        if (P[0].second == 1) {
            if (P[1].second == 1) {
                flip(0, P[0].second + P[1].second + P[2].second);
            } else {
                flip(0, 2);
            }
            flip(0, 1);
        }
    }
    cout << S << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int T;
    cin >> T;
    while (T--) solve();
}
0