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

Solution 1 (runtime: 46 ms):
#include <bits/stdc++.h>

using namespace std;



#define divyaraj                  \

    ios_base::sync_with_stdio(0); \

    cin.tie(0);

#define ll long long int

#define YES cout << "YES\n"

#define pb emplace_back

#define NO cout << "NO\n"

#define fi(x, y) for (ll i = x; i < y; i++)

#define fj(x, y) for (ll j = x; j < y; j++)

#define fk(x, y) for (ll k = x; k < y; k++)

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

#define allr(v) (v.rbegin()), (v.rend())

#define nl '\n'

#define pnl(x) cout << x << '\n'

#define p(x) cout << x << ' '

#define inpv(v)        \

    for (auto &it : v) \

    cin >> it

#define vi vector<int>

#define vll vector<long long int>



void code()

{

    ll n;

    cin >> n;

    string s;

    cin >> s;

    ll p = 0, _s = 0;

    for (auto &ch : s)

    {

        ch == 'p' ? p++ : _s++;

    }

    if (p == n || _s == n || (p + _s) == 0)

    {

        pnl("YES");

        return;

    }

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

    {

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

        {

            int j = i + 1;

            while (j < n)

            {

                if (s[j] == 's')

                {

                    pnl("NO");

                    return;

                }

                j++;

            }

        }

    }

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

    {

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

        {

            int j = i + 1;

            while (j < n - 1)

            {

                if (s[j] == 'p')

                {

                    pnl("NO");

                    return;

                }

                j++;

            }

        }

    }

    pnl("YES");

}



signed main(int argc, char *argv[])

{

    // #ifndef ONLINE_JUDGE

    // freopen("input.txt","r", stdin);

    // freopen("output.txt","w", stdout);

    // #endif

    divyaraj;

    int t = 1;

    cin >> t;

    while (t--)

        code();

}

Solution 2 (runtime: 62 ms):
#include <bits/stdc++.h>

using namespace std;

#define fastio() ios::sync_with_stdio(false);cin.tie(nullptr);

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

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

#define pb push_back

#define fi first

#define se second

#define ll long long

const long long LINF = 1e18;

const int MOD = 1000000007;  

#define debug(x) cerr << #x << '=';_print(x);cerr << endl;

void _print(int x) { cerr << x; }

void _print(long long x) { cerr << x; }

void _print(double x) { cerr << x; }

void _print(char x) { cerr << x; }

void _print(const string &x) { cerr << x; }

void _print(bool x) { cerr << (x ? '1' : '0'); }

template <typename T>

void _print(const vector<T> &v)

{cerr << '[';for (const auto &i : v){_print(i);cerr << ' ';}cerr << ']';}

template <typename T>

void _print(const set<T> &s){cerr << '{';for (const auto &i : s){_print(i);cerr << ' ';}cerr << '}';}

template <typename K, typename V>

void _print(const map<K, V> &m){cerr << '{'<<endl;for (const auto &p : m){cerr << '(';_print(p.first);cerr << ':';_print(p.second);cerr << ')';}cerr << endl<<'}';}

template <typename T1, typename T2>

void _print(const pair<T1, T2> &p){cerr << '(';_print(p.first);cerr << ',';_print(p.second);cerr << ')';}

ll mod_add(ll&a,ll&b,ll m) {return (a+b)%m;}

ll mod_mul(ll&a,ll&b, ll m) {return (a*b)%m;}

ll mod_sub(ll&a,ll&b,ll m) {return ((a-b)%m+m)%m;}

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

ll binExpo(ll a,ll n) {ll res=1;while(n) {if(n&1) {res=(res*a)%MOD;}a=(a*a)%MOD; n>>=1;}return res;}

ll modInv(ll a,ll mod) {return binExpo(a,mod-2);}

int N;

vector<ll>fact(N+2,0);

void calcFact() {fact[0]=1;fact[1]=1;for(ll i=2;i<=N;i++){fact[i]=mod_mul(fact[i-1],i,MOD);}}

ll ncr(ll n,ll r) {ll numr=fact[n];ll den=mod_mul(fact[n-r],fact[r],MOD);ll ans=(modInv(den,MOD)*numr)%MOD;return ans;}

/*<------------------------------------------------------------------------>*/

void solve() {

    ll n;

    cin>>n;

    string s;

    cin>>s;

    bool ok=1;

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

        if(s[i]=='s' and i==0) continue;

        if(s[i]=='.') continue;

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

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

                ok=0;

                cout<<"NO"<<endl;

                return ;

            }

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

                cout<<"NO"<<endl;

                return ;

            }



        }

    }

    cout<<"YES"<<endl;



}

int main()

{

    fastio()

    ll int t;

    cin >> t;

    while(t--) solve();

    return 0;

}