Problem: 2049B - pspspsps
Language: c++20 

Code 1:
#include <bits/stdc++.h>

using namespace std;

 

typedef long long ll;

typedef unsigned long long ull; 

typedef pair<int, int> pii;

typedef pair<double, double> pdd;

typedef pair<long long, long long> pll;

typedef vector<int> vi;

typedef vector<long long> vll;

typedef map<int, int> mii;

typedef vector<vector<int>> vvi;

typedef vector<vector<long long>> vvll;

#define f(i, a, b, j) for(int i = int(a); i < int(b); i = i + int(j))

#define M map

#define V vector

#define P pair

#define all(a) a.begin(), a.end() 

#define rall(a) a.rbegin(), a.rend() 

#define nl "\n"

#define pb push_back

#define mp make_pair

#define reset(a, b) memset(a, int(b), sizeof(a))

#define MOD1 (ll)1000000007

#define MOD2 (ll)998244353



ll mod = MOD1;

ll power(ll a,ll b){

    ll res = 1;

    while(b){

        if(b & 1){

            res *= a;

            res %= mod;

        }

        a *= a;

        a %= mod;

         b >>= 1;

    }

    return res;

}

ll inverse(ll a){

    return power(a,mod - 2);

}



void solve(){

    ll n;

    cin >> n;

    string s;

    cin >> s;

    // p before s (not possible)

    ll r_s = n;

    for(int i = n - 1; i >= 0; i--){

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

            r_s = i;

            break;

        }

    }

    if(r_s == n){

        cout << "YES" << nl;

        return;

    }

    ll l_p = -1;

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

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

            l_p = i;

            break;

        }

    }

    if(l_p == -1){

        cout << "YES" << nl;

        return;

    }

    if(l_p < r_s){

        cout << "NO" << nl;

        return;

    }

    ll count_s = 0;

    ll count_p = 0;

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

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

            count_s++;

        }

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

            count_p++;

        }

    }

    if(count_s > 1 and count_p > 1){

        cout << "NO" << nl;

        return;

    }

    if(count_s == 1 and count_p == 1){

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

            cout << "NO" << nl;

            return;

        }

        cout << "YES" << nl;

        return;

    }

    if(count_s == 1){

        // more than one p 

        if(s[0] != 's'){

            cout << "NO" << nl;

            return;

        }

        cout << "YES" << nl;

        return;

    }

    // count_p == 1

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

        cout << "NO" << nl;

        return;

    }

    cout << "YES" << nl;

    return;

}

int main(){

    ios_base::sync_with_stdio(false);

    cin.tie(NULL);

    ll t;

    cin >> t;

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

        solve();

    }

    return 0;

}

Code 2:
#include <bits/stdc++.h>

using namespace std;

 

typedef long long ll;

typedef unsigned long long ull; 

typedef pair<int, int> pii;

typedef pair<double, double> pdd;

typedef pair<long long, long long> pll;

typedef vector<int> vi;

typedef vector<long long> vll;

typedef map<int, int> mii;

typedef vector<vector<int>> vvi;

typedef vector<vector<long long>> vvll;

#define f(i, a, b, j) for(int i = int(a); i < int(b); i = i + int(j))

#define M map

#define V vector

#define P pair

#define all(a) a.begin(), a.end() 

#define rall(a) a.rbegin(), a.rend() 

#define nl "\n"

#define pb push_back

#define mp make_pair

#define reset(a, b) memset(a, int(b), sizeof(a))

#define MOD1 (ll)1000000007

#define MOD2 (ll)998244353



ll mod = MOD1;

ll power(ll a,ll b){

    ll res = 1;

    while(b){

        if(b & 1){

            res *= a;

            res %= mod;

        }

        a *= a;

        a %= mod;

         b >>= 1;

    }

    return res;

}

ll inverse(ll a){

    return power(a,mod - 2);

}



void solve(){

    ll n;

    cin >> n;

    string s;

    cin >> s;

    // p before s (not possible)

    ll r_s = n;

    for(int i = n - 1; i >= 0; i--){

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

            r_s = i;

            break;

        }

    }

    if(r_s == n){

        cout << "YES" << nl;

        return;

    }

    ll l_p = -1;

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

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

            l_p = i;

            break;

        }

    }

    if(l_p == -1){

        cout << "YES" << nl;

        return;

    }

    if(l_p < r_s){

        cout << "NO" << nl;

        return;

    }

    ll count_s = 0;

    ll count_p = 0;

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

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

            count_s++;

        }

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

            count_p++;

        }

    }

    if(count_s > 1 and count_p > 1){

        cout << "NO" << nl;

        return;

    }

    if(count_s == 1){

        // more than one p 

        if(s[0] != 's'){

            cout << "NO" << nl;

            return;

        }

        cout << "YES" << nl;

        return;

    }

    // count_p == 1

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

        cout << "NO" << nl;

        return;

    }

    cout << "YES" << nl;

    return;

}

int main(){

    ios_base::sync_with_stdio(false);

    cin.tie(NULL);

    ll t;

    cin >> t;

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

        solve();

    }

    return 0;

}

Answer: 1