Untitled

 avatar
unknown
plain_text
a year ago
1.3 kB
15
Indexable
//Author: Jigyasu

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

#define xlr8 ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
#define ll long long
#define endl "\n"
#define vi vector<int>
#define vs vector<string>
#define vll vector<long long int>
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(a) a.begin(), a.end()
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) a*b/gcd(a,b)
#define yes(check) (check) ? cout << "YES" << endl : cout << "NO" << endl
#define rep(n) for(int i = 0; i < n; i++)

bool checkAB(string s, int n){
    for(int i = 0; i < n-1; i++){
        if(s[i] == 'A' and s[i+1] == 'B') return false;
    }
    return true;
}

void solve(){
    int n; cin >> n;
    string s; cin >> s;
    int count = 0;
    int i = 0;
    bool flag = false;
    while(true){
        if(checkAB(s, n)) break;
        for(int i = 0; i < n-1; i++){
            if(s[i] == 'A' and s[i+1] == 'B'){
                count++;
                swap(s[i], s[i+1]);
                i++;
                if(i == n-1) flag = true; 
            }
        }
        if(flag) break;
    }
    cout << count << endl;
}


signed main(){
    xlr8;

    int tt; cin >> tt;
    while(tt--) 
    solve();
    return 0;
}
Editor is loading...
Leave a Comment