結果

問題 No.1894 Delete AB
ユーザー milanis48663220milanis48663220
提出日時 2022-04-08 21:38:10
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 29 ms / 2,000 ms
コード長 2,906 bytes
コンパイル時間 1,493 ms
コンパイル使用メモリ 124,384 KB
実行使用メモリ 17,472 KB
最終ジャッジ日時 2024-05-06 07:10:29
合計ジャッジ時間 2,389 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 10 ms
15,616 KB
testcase_02 AC 10 ms
15,488 KB
testcase_03 AC 23 ms
5,376 KB
testcase_04 AC 23 ms
5,376 KB
testcase_05 AC 25 ms
5,376 KB
testcase_06 AC 25 ms
5,376 KB
testcase_07 AC 25 ms
5,376 KB
testcase_08 AC 29 ms
5,376 KB
testcase_09 AC 9 ms
5,376 KB
testcase_10 AC 10 ms
5,376 KB
testcase_11 AC 7 ms
5,376 KB
testcase_12 AC 7 ms
5,520 KB
testcase_13 AC 18 ms
17,472 KB
testcase_14 AC 10 ms
15,300 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
    return vector<vector<T>>(n, vector<T>(m, v));
}

template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
    return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}

template<typename T>
void print_vector(vector<T> v, char delimiter=' '){
    if(v.empty()) {
        cout << endl;
        return;
    }
    for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter;
    cout << v.back() << endl;
}


template <typename T>
class OffsetVec{
    public:
	int n;
    vector<T> v;
	T& operator[](int x) {
        return v[x+n];
	}
    void print(){
        for(int i = -n; i <= n; i++) cout << v[i+n] << ' ';
        cout << endl;
    }
    OffsetVec(int _n){
        n = _n;
        v = vector<T>(2*n+1);
    }
    OffsetVec(int _n, T x){
        n = _n;
        v = vector<T>(2*n+1, x);
    }
};

void solve(){
    int n; cin >> n;
    string s; cin >> s;
    vector<int> x(n);
    for(int i = 0; i < n; i++) {
        if(s[i] == 'A') x[i] = 1;
        else x[i] = -1;
    }
    vector<int> cumsum(n+1);
    for(int i = 0; i < n; i++) cumsum[i+1] = cumsum[i]+x[i];
    OffsetVec<vector<int>> indices(n+1);
    for(int i = 0; i <= n; i++){
        indices[cumsum[i]].push_back(i);
    }
    for(int x = -n; x <= n; x++) reverse(indices[x].begin(), indices[x].end());
    int cur = 0;
    string ans;
    while(cur < n){
        // debug_value(cur);
        if(s[cur] == 'B'){
            ans += 'B';
            cur++;
        }else{
            int x = cumsum[cur];
            // debug_value(x)
            if(indices[x-1].empty()){
                ans += 'A';
                cur++;
            }else{
                while(!indices[x-1].empty() && indices[x-1].back() <= cur) indices[x-1].pop_back();
                if(!indices[x-1].empty()){
                    ans += 'B';
                    cur = indices[x-1].back();
                }else{
                    ans += 'A';
                    cur++;
                }
            }
        }
    }
    cout << ans << endl;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int t; cin >> t;
    while(t--) solve();
}
0