Problem: 2049B - pspspsps
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define mk make_pair
#define pb push_back
#define INF (ll)1e18
#define pll pair<ll, ll>
#define vll vector<ll>
#define vii vector<int>
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define isON(n, k) (((n) >> (k)) & 1)
#define f(i, a, b) for (ll i = a; i < b; i++)
#define fb(i, a, b) for (ll i = a; i > b; i--)
#define ff first
#define ss second
#define PI 3.141592653589793238
// int dx[8] = { 1, 0, -1, 0, -1, 1, -1, 1 };
// int dy[8] = { 0, 1, 0, -1, -1, 1, 1, -1 };
using namespace std;
const int N = 2e5 + 5;
const ll MOD = 1e9 + 7;
ll t = 1;

void solve()
{
    ll n;
    cin >> n;
    string s;
    cin >> s;
    ll count_p = count(s.begin(), s.end(), 'p');
    ll count_s = count(s.begin(), s.end(), 's');
    if (count_p == 0 || count_s == 0){
        cout << "YES\n";
    }
    else {
        if (s[0] == 's' || s[n - 1] == 'p'){
            bool f = 0;
            f(i,0,n){
                if (s[i] == 'p')
                    f = 1;
                if (f && s[i] == 's'){
                    cout << "NO\n";
                    return;
                }
            }
            cout << "YES\n";
        }
        else {
            cout << "NO\n";
        }
    }
    return;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin >> t;
    while (t--)
    {
        solve();
    }

    return 0;
}

Submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define mk make_pair
#define pb push_back
#define INF (ll)1e18
#define pll pair<ll, ll>
#define vll vector<ll>
#define vii vector<int>
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define isON(n, k) (((n) >> (k)) & 1)
#define f(i, a, b) for (ll i = a; i < b; i++)
#define fb(i, a, b) for (ll i = a; i > b; i--)
#define ff first
#define ss second
#define PI 3.141592653589793238
// int dx[8] = { 1, 0, -1, 0, -1, 1, -1, 1 };
// int dy[8] = { 0, 1, 0, -1, -1, 1, 1, -1 };
using namespace std;
const int N = 2e5 + 5;
const ll MOD = 1e9 + 7;
ll t = 1;

void solve()
{
    ll n;
    cin >> n;
    string s;
    cin >> s;
    // Check if the string contains 's' or 'p'
    if (s.find('s') == string::npos && s.find('p') == string::npos) {
        cout << "YES\n";
        return;
    }
    int l = -1, r = -1;
    f(i,0,n){
        switch (s[i])
        {
        case 'p':
            if (l == -1)
            {
                l = 1;
                r = i+1;
            }
            else {
                int ff = 1, ss = i+1;
                // Check if the new range [ff, ss] can contain the range [l, r] or vice versa
                if ((ff <= l && ss >= r) || (l <= ff && r >= ss))
                {
                    l = min(l, ff);
                    r = max(r, ss);
                }
                else
                {
                    cout << "NO\n";
                    return;
                }
            }
            break;
        case 's':
            if (l == -1)
            {
                l = i+1;
                r = n;
            }
            else {
                int ff = i+1, ss = n;
                // Check if the new range [ff, ss] can contain the range [l, r] or vice versa
                if ((ff <= l && ss >= r) || (l <= ff && r >= ss))
                {
                    l = min(l, ff);
                    r = max(r, ss);
                }
                else
                {
                    cout << "NO\n";
                    return;
                }
            }
            break;
        default:
            break;
        }
    }
    cout << "YES\n";

    return;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin >> t;
    while (t--)
    {
        solve();
    }

    return 0;
}