/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB
#2 Accepted 18ms 564.0 KiB
#3 Accepted 19ms 568.0 KiB
#4 Accepted 22ms 556.0 KiB
#5 Accepted 7ms 768.0 KiB
#6 Accepted 6ms 540.0 KiB
#7 Accepted 7ms 556.0 KiB
#8 Accepted 7ms 1.027 MiB
#9 Accepted 8ms 780.0 KiB
#10 Accepted 7ms 796.0 KiB

Code

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
#define bug(x) cout<<#x<<" : "<<x<<endl;
#define bug2(a,b) cout<<#a<<" : "<<a<<"\t"<<#b<<" : "<<b<<endl;

int n;
string a,b;
const int N=1e5+7,M=200;
// int dp[N][M];
// vector<int>aa,bb;

// int f(int i,char last){
// 	if(i==n){
// 		return 1;
// 	}
// 	if(dp[i][(last)]!=-1)return dp[i][(last)];
// 	int ans=0;
// 	if((last)<=(b[i]))ans=max(ans,f(i+1,(b[i]-'a')));
// 	if((last-'a')<=(a[i]-'a'))ans=max(ans,f(i+1,(a[i]-'a')));
// 	// bug(last)
// 	// bug(ans)
// 	return dp[i][(last-'a')]=ans;
// }
signed main(){
	
	ll t=1;cin>>t;
	while(t--){
		cin>>n;
		cin>>a>>b;
		string tmpa="",tmpb="";
		tmpa+=a[0];
		tmpb+=b[0];
		// for(int i=0;i<n;i++){

		// }

		char last=a[0];
		bool ok=0;
		for(int i=1;i<n;i++){
			if(last<=a[i] and last<=b[i]){
				if(a[i]<b[i]){
					last=a[i];
					tmpa+=a[i];
				}
				else {
					last=b[i];
					tmpa+=b[i];
				}
			}
			else if(last<=a[i]){
				last=a[i];
				tmpa+=a[i];
			}
			else if(last<=b[i]){
				last=b[i];
				tmpa+=b[i];
			}
			else break;
		}
		last=b[0];
		// mp.clear();

		for(int i=1;i<n;i++){
			if(last<=a[i] and last<=b[i]){
				if(a[i]<b[i]){
					last=a[i];
					tmpb+=a[i];
				}
				else {
					last=b[i];
					tmpb+=b[i];
				}
			}
			else if(last<=a[i]){
				last=a[i];
				tmpb+=a[i];
			}
			else if(last<=b[i]){
				last=b[i];
				tmpb+=b[i];
			}
			else break;
		}
		// bug(dis)
		// bug(tmpa)
		// bug(tmpb)
		if(is_sorted(tmpb.begin(),tmpb.end()) and tmpb.size()==n)ok=1;
		if(is_sorted(tmpa.begin(),tmpa.end()) and tmpa.size()==n)ok=1;
		if(ok)cout<<"Yes"<<endl;
		else cout<<"No"<<endl;
	}
	return 0;
}

Information

Submit By
Type
Submission
Problem
P1117 String Sorted
Contest
LU IUJPC : Sylhet Division 2024, Mock Round
Language
C++17 (G++ 13.2.0)
Submit At
2024-12-07 11:10:34
Judged At
2024-12-07 11:10:34
Judged By
Score
100
Total Time
22ms
Peak Memory
1.027 MiB