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

1672B - I love AAAB
1673A - Subtle Substring Subtraction
1345A - Puzzle Pieces
711A - Bus to Udayland
779B - Weird Rounding
1703D - Double Strings
1704C - Virus
63A - Sinking Ship
1704B - Luke is a Foodie
298B - Sail
239A - Two Bags of Potatoes
1704E - Count Seconds
682A - Alyona and Numbers
44A - Indian Summer
1133C - Balanced Team
1704A - Two 0-1 Sequences
1467A - Wizard of Orz
1714E - Add Modulo 10
1714A - Everyone Loves to Sleep
764A - Taymyr is calling you
1714B - Remove Prefix
1264F - Beautiful Fibonacci Problem
52A - 123-sequence
1543A - Exciting Bets
1714D - Color with Occurrences
215B - Olympic Medal
1445A - Array Rearrangment
1351A - A+B (Trial Problem)
935B - Fafa and the Gates
1291A - Even But Not Even