Problem: 2049B - pspspsps
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>

#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)

#define vll vector<long long>

#define ll long long

#define pll pair<ll, ll>

#define mll map<ll, ll>

using namespace std;



int gcd(ll a, ll b) {

    return b == 0 ? a : gcd(b, a % b);

}



long long lcm(long long a, long long b) {

    return (a * b) / gcd(a, b);

}



vector<long long> sieve_of_eratosthenes(long long n) {

    if (n < 2) return {};



    vector<bool> is_prime(n + 1, true);

    is_prime[0] = is_prime[1] = false;



    for (long long p = 2; p * p <= n; ++p) {

        if (is_prime[p]) {

            for (long long i = p * p; i <= n; i += p) {

                is_prime[i] = false;

            }

        }

    }



    vector<long long> primes;

    for (long long p = 2; p <= n; ++p) {

        if (is_prime[p]) {

            primes.push_back(p);

        }

    }

    return primes;

}



ll nCr(ll n, ll r) {

    if (r > n) return 0;

    if (r > n - r) r = n - r; // Because C(n, r) == C(n, n - r)

    ll result = 1;

    for (ll i = 0; i < r; ++i) {

        result *= (n - i);

        result /= (i + 1);

    }

    return result;

}



int findGCD(vector<ll> arr) {

    int result = arr[0];

    for (int i = 1; i < arr.size(); i++) {

        result = gcd(result, arr[i]);

    }

    return result;

}



ll binarySearch(const vector<ll>& arr, ll x) {

    ll left = 0;

    ll right = arr.size() - 1;



    while (left <= right) {

        ll mid = left + (right - left) / 2;



        if (arr[mid] == x)

            return mid;



        if (arr[mid] < x)

            left = mid + 1;

        else

            right = mid - 1;

    }



    return -1; // Element not found

}



bool sortBySecond(const pair<ll, ll>& a, const pair<int, int>& b) {

    return a.second < b.second;

}



void sortBySecondVOP(vector<pair<ll, ll>>& vec) {

    sort(vec.begin(), vec.end(), sortBySecond);

}



void solve() {

    ll n;

    cin >> n;

    string s;

    cin >> s;

    bool fs = false;

    bool fp = false;

    for (int i = 0; i < n; ++i) {

        if(s[i] == 'p') fp = 1;

        if(s[i] == 's') fs = 1;

        if(fp && s[i] == 's'){

            cout<<"NO"<<endl;

            return;

        }

    }

    if(s[0] != 's'&& s[n-1]!='p'){

        ll pc = 0;

        ll sc = 0;

        for (int i = 1; i < n-1; ++i) {

            if(s[i] == 'p') ++pc;

            else if(s[i]=='s') ++sc;

        }

        if(sc>0 && pc>0){

            cout<<"NO"<<endl;

            return;

        }

    }

    

    cout<<"YES"<<endl;

    // if(s[0]=='s' || s[n-1]=='p')

    //     cout<<"YES"<<endl;

    // else

    //     cout<<"NO"<<endl;



}



int main() {

    fast;

    ll t;

    cin >> t;

    while (t--) {

        solve();

    }

    return 0;

}

Submission (verdict: Accepted):
#include <bits/stdc++.h>

#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)

#define vll vector<long long>

#define ll long long

#define pll pair<ll, ll>

#define mll map<ll, ll>

using namespace std;



int gcd(ll a, ll b) {

    return b == 0 ? a : gcd(b, a % b);

}



long long lcm(long long a, long long b) {

    return (a * b) / gcd(a, b);

}



vector<long long> sieve_of_eratosthenes(long long n) {

    if (n < 2) return {};



    vector<bool> is_prime(n + 1, true);

    is_prime[0] = is_prime[1] = false;



    for (long long p = 2; p * p <= n; ++p) {

        if (is_prime[p]) {

            for (long long i = p * p; i <= n; i += p) {

                is_prime[i] = false;

            }

        }

    }



    vector<long long> primes;

    for (long long p = 2; p <= n; ++p) {

        if (is_prime[p]) {

            primes.push_back(p);

        }

    }

    return primes;

}



ll nCr(ll n, ll r) {

    if (r > n) return 0;

    if (r > n - r) r = n - r; // Because C(n, r) == C(n, n - r)

    ll result = 1;

    for (ll i = 0; i < r; ++i) {

        result *= (n - i);

        result /= (i + 1);

    }

    return result;

}



int findGCD(vector<ll> arr) {

    int result = arr[0];

    for (int i = 1; i < arr.size(); i++) {

        result = gcd(result, arr[i]);

    }

    return result;

}



ll binarySearch(const vector<ll>& arr, ll x) {

    ll left = 0;

    ll right = arr.size() - 1;



    while (left <= right) {

        ll mid = left + (right - left) / 2;



        if (arr[mid] == x)

            return mid;



        if (arr[mid] < x)

            left = mid + 1;

        else

            right = mid - 1;

    }



    return -1; // Element not found

}



bool sortBySecond(const pair<ll, ll>& a, const pair<int, int>& b) {

    return a.second < b.second;

}



void sortBySecondVOP(vector<pair<ll, ll>>& vec) {

    sort(vec.begin(), vec.end(), sortBySecond);

}



void solve() {

    ll n;

    cin >> n;

    string s;

    cin >> s;

    bool fs = false;

    bool fp = false;

    for (int i = 0; i < n; ++i) {

        if(s[i] == 'p') fp = 1;

        if(s[i] == 's') fs = 1;

        if(fp && s[i] == 's'){

            cout<<"NO"<<endl;

            return;

        }

    }

    fs = false;

    fp = false;

    for (int i = 1; i < n-1; ++i) {

        if(s[i] == 'p') fp = 1;

        if(s[i] == 's') fs = 1;

        if(fs && s[i] == 'p'){

            cout<<"NO"<<endl;

            return;

        }

    }

    if(s[0] != 's' && s[n-1]!='p'){

        ll pc = 0;

        ll sc = 0;

        for (int i = 0; i < n; ++i) {

            if(s[i] == 'p') ++pc;

            else if(s[i]=='s') ++sc;

        }

        if(sc>0 && pc>0){

            cout<<"NO"<<endl;

            return;

        }

    }

    cout<<"YES"<<endl;

}



int main() {

    fast;

    ll t;

    cin >> t;

    while (t--) {

        solve();

    }

    return 0;

}