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

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

using namespace std;

int main()

{

    int t;

    cin >> t;

    while (t--)

    {

        int n;

        cin >> n;

        string s;

        cin >> s;





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

            s[0] = '.';

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

            s[n - 1] = '.';

        bool fp=0, fs=0;

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

        {

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

            {

                fs=1;

            }

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

            {

                fp=1;

            }

        }

        if (fs==1 && fp==1)

            cout << "NO" << endl;

        else

            cout << "YES" << endl;

    }

    return 0;

}

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

typedef long long ll;

#define get(a, n) for (ll i = 0; i < n; i++) cin >> a[i]

using namespace std;

 

void pv(vector<int> a){

	for(ll i=0;i<a.size();i++){

		cout<<a[i]<<' ';

	}cout<<endl;

}

void haha(ll a,vector<int> &ap){

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

		ap[i]=a%2;

		a=a/2;

	}

}

ll fact(int a){

	ll ans=1;

	for(int i=2;i<a;i++){

		ans*=i;

	}return ans;

}

ll ncr(int a,int b){

	return fact(a)/(fact(b)*fact(a-b));

}

template <typename T>

class MapNode{

	public:

	string key;

	T value;

	MapNode<T>* next;



	public:

	MapNode(string key,T value){

		this->key=key;

		this->value=value;

		next=NULL;

	}

	~MapNode(){

		delete next;

	}

};

template <typename T>

class HashMap{

	MapNode<T>** buckets;

	int sizebucket;

	int count;

	int hashfunc(string key){

		int hashed=0;

		int coeff=1;

		for(int i=key.size()-1;i>=0;i--){

			hashed=(hashed+key[i]*coeff)%sizebucket;

			coeff=(coeff*37)%sizebucket;

		}return hashed;

	}

	void rehash(){

		MapNode<T> **temp=buckets;

		buckets=new MapNode<T>*[2*sizebucket];

		for(int i=0;i<2*sizebucket;i++){

			buckets[i]=NULL;

		}

		int tempbucketsize=sizebucket;

		sizebucket=2*sizebucket;

		count=0;

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

			MapNode<T>* head=temp[i];

			while(head!=NULL){

				insert(head->key,head->value);

				head=head->next;

			}

		}return;

	}

public:

	double getloadfactor(){

		return (1.0*count/sizebucket);

	}

	public:

	HashMap(){

		buckets=new MapNode<T>*[5];

		sizebucket=5;

		count=0;

	}

	int size(){

		return count;

	}

	T search(string key){

		int indexx=hashfunc(key);

		MapNode<T>* head=buckets[indexx];

		while(head!=NULL){

			if(head->key==key){

				return head->value;

			}head=head->next;

		}return 0;

	}

	void insert(string key,T value){

		int indexx=hashfunc(key);

		MapNode<T>* head=buckets[indexx];

		while(head!=NULL){

			if(head->key==key){

				head->value=value;

				return;

			}head=head->next;

		}head=buckets[indexx];

		MapNode<T>* temp=new MapNode<T>(key,value);

		temp->next=head;

		buckets[indexx]=temp;

		count++;

		if(getloadfactor()>0.7){

			rehash();

		}

		return;

	}

	T remove(string key){

		int indexx=hashfunc(key);

		MapNode<T>* head=buckets[indexx];

		MapNode<T>* prev=NULL;

		while(head!=NULL){

			if(head->key==key){

				if(prev==NULL){

					buckets[indexx]=head->next;

				}else{

					prev->next=head->next;

				}head->next=NULL;

				T value=head->value;

				delete head;

				count--;

				return value;

			}prev=head;

			head=head->next;

		}return 0;

	}

	~HashMap(){

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

			delete buckets[i];

		}delete [] buckets;

	}

};



class TrieNode{

public:

	char data;

	bool isTerminal;

	TrieNode** children;



	TrieNode(char data){

		this->data=data;

		isTerminal=false;

		children=new TrieNode*[26];

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

			children[i]=NULL;

		}

	}

	~TrieNode(){

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

			delete children[i];

		}

	}

};

class Trie{

	TrieNode* root;

public:



	void inserthelper(string word,TrieNode* root){

		if(word.size()==0){

			root->isTerminal=true;

			return;

		}

		int indexx=word[0]-'a';

		TrieNode* child;

		if(root->children[indexx]==NULL){

			child=new TrieNode(word[0]);

			root->children[indexx]=child;

		}else{

			child=root->children[indexx];

		}

		inserthelper(word.substr(1),child);

	}

	void removehelper(string word,TrieNode* root){

		if(word.size()==0){

			root->isTerminal=false;

			return;

		}int indexx=word[0]-'a';

		TrieNode* child;

		if(root->children[indexx]==NULL){

			return;

		}child=root->children[indexx];

		removehelper(word.substr(1),child);

		if(!child->isTerminal){

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

				if(child->children[i]!=NULL){

					return;

				}

			}delete child;

			root->children[indexx]=NULL;

			return;

		}

	}

public:

	Trie(){

		root=new TrieNode('\0');

	}

	void insert(string word){

		inserthelper(word,root);

	}void remove(string word){

		removehelper(word,root);

	}

};

bool triangle(int a,int b,int c){

	if((a+b)	<c || (b+c)<a || a+c<b){

		return false;

	}return true;

}



int main(){

	int t=1;

	cin>>t;

	while(t--){

		int n;

		cin>>n;

		string s;

		cin>>s;

		bool ans=false;

		if(s[0]=='s') s[0]='.';

		if(s[n-1]=='p') s[n-1]='.';

		int ctp=0,cts=0;

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

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

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

		}

		if(ctp==0 || cts==0){

			ans=true;

		}

		if(ans) cout<<"YES"<<endl;

		else cout<<"NO"<<endl;

	}

}

//0 0 0 0 1 1 0