You are given two strings ss and tt, both of length nn. Each character in both string is 'a', 'b' or 'c'.
In one move, you can perform one of the following actions:
- choose an occurrence of "ab" in ss and replace it with "ba";
- choose an occurrence of "bc" in ss and replace it with "cb".
You are allowed to perform an arbitrary amount of moves (possibly, zero). Can you change string ss to make it equal to string tt?
Input
The first line contains a single integer qq (1≤q≤1041≤q≤104) — the number of testcases.
The first line of each testcase contains a single integer nn (1≤n≤1051≤n≤105) — the length of strings ss and tt.
The second line contains string ss of length nn. Each character is 'a', 'b' or 'c'.
The third line contains string tt of length nn. Each character is 'a', 'b' or 'c'.
The sum of nn over all testcases doesn't exceed 105105.
Output
For each testcase, print "YES" if you can change string ss to make it equal to string tt by performing an arbitrary amount of moves (possibly, zero). Otherwise, print "NO".
Example
input
Copy
5 3 cab cab 1 a b 6 abbabc bbaacb 10 bcaabababc cbbababaac 2 ba ab
output
Copy
YES NO YES YES NOs
思路:
1,透过操作要求,找到一些相对不变的规则
2,读假题了,“ab”是个连续的字串,因此a只能相对后移,c只能相对的前移
3,代码是通过验证通过的,需要符合2,以及去掉b后的字符串要一致
代码:
#include<bits/stdc++.h>
using namespace std;
#define int long long
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")//
const int maxj=3e5+100,mod=998244353;
void solve(){//构造验证,a的位置只会后移,c的位置只会前移,a,c的个数各自相同
int n;cin>>n;
string s,t;
cin>>s>>t;
int cnt1=0,cnt2=0;
int ct1=0,ct2=0;
vector<int>a1,a2,c1,c2;
string ss1="",ss2="";
for(int i=0;i<n;++i){
if(s[i]!='b') ss1+=s[i];
if(t[i]!='b') ss2+=t[i];
if(s[i]=='a') a1.emplace_back(i);
if(s[i]=='c') c1.emplace_back(i);
if(t[i]=='a') a2.emplace_back(i);
if(t[i]=='c') c2.emplace_back(i);
}
if(ss1!=ss2){//a,c相对位置不变,abc,cba这个不做考虑
cout<<"NO"<<'\n';
return ;
}
bool k=0;
for(int i=0;i<a1.size();++i){
if(a1[i]>a2[i]){
cout<<"NO"<<'\n';
return ;
}
}
for(int i=0;i<c1.size();++i){
if(c1[i]<c2[i]){
cout<<"NO"<<'\n';
return ;
}
}
cout<<"YES"<<'\n';
}
int32_t main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
// t=1;
while(t--)solve();
return 0;
}