1754A - Technical Support - CodeForces Solution


*800

Please click on ads to support us..

Python Code:

def solve():
    _ = int(input())
    t = input()
    ctn = 0
    for i in t:
        if i == 'Q':
            ctn += 1
        else:
            ctn -= 1

        if ctn < 0:
            ctn = 0
    return "Yes" if ctn == 0 else "No"


if __name__ == "__main__":
    t = int(input())
    for _ in range(t):
        print(solve())

C++ Code:

//MANISHk
#include<bits/stdc++.h>
using namespace std;
#define ll long long

#define db double
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define all(v) v.begin(), v.end()
#define min3(a, b, c) min(c, min(a, b))
#define min4(a, b, c, d) min(d, min(c, min(a, b)))
#define frr(i, k, n) for (int i = n - 1; i >= k; i--)
#define fr(i, k, n) for (int i = k; i < n; i++)


long long f(int a, int b) {
if(b == 0) return 1;
long long p = f(a,b/2);
if(b%2==0) return (p*p)%1000000007;
else return (((p*p)%1000000007) * a) % 1000000007;
}


ll gcd(ll a, ll b){
if (a == 0)
return b;
if (b == 0)
return a;
if (a < b)
return gcd(a, b % a);
return gcd(b, a % b);
}

ll lcm (ll a , ll b){
return (a*b) / gcd(a , b);
}

bool isperfect(double n){
if(n >= 0){
ll x = sqrt(n);
return (x*x == n);
}
else
return false;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
ll gcdExtended(ll a, ll b, ll *x, ll *y);

ll modInverse(ll b, ll m){
ll x, y;
ll g = gcdExtended(b, m, &x, &y);
if (g != 1)
return -1;
return (x%m + m) % m;
}

ll modDivide(ll a, ll b, ll m){
a = a % m;
ll inv = modInverse(b, m);
return (inv * a) % m;
}

ll gcdExtended(ll a, ll b, ll *x, ll *y){
if (a == 0){
*x = 0, *y = 1;
return b;
}

ll x1;ll y1;
ll gcd = gcdExtended(b%a, a, &x1, &y1);

*x = y1 - (b/a) * x1;
*y = x1;
return gcd;
}


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void solve(){
    ll n;
    cin>>n;
    string s;
    cin>>s;
    ll qc=0,ac=0;
    for(int i=0;i<n;i++){
        if(s[i]=='Q')qc++;
        else qc--;
        if(qc<0)qc=0;
    }
    if(qc!=0)cout<<"No"<<endl;
    else cout<<"Yes"<<endl;
   // cout<<qc<<ac<<endl;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main(){
ll t;
cin>>t;
while(t--){
solve();
}
}


Comments

Submit
0 Comments
More Questions

1271A - Suits
259B - Little Elephant and Magic Square
1389A - LCM Problem
778A - String Game
1382A - Common Subsequence
1512D - Corrupted Array
667B - Coat of Anticubism
284B - Cows and Poker Game
1666D - Deletive Editing
1433D - Districts Connection
2B - The least round way
1324A - Yet Another Tetris Problem
246B - Increase and Decrease
22E - Scheme
1566A - Median Maximization
1278A - Shuffle Hashing
1666F - Fancy Stack
1354A - Alarm Clock
1543B - Customising the Track
1337A - Ichihime and Triangle
1366A - Shovels and Swords
919A - Supermarket
630C - Lucky Numbers
1208B - Uniqueness
1384A - Common Prefixes
371A - K-Periodic Array
1542A - Odd Set
1567B - MEXor Mixup
669A - Little Artem and Presents
691B - s-palindrome