Time Exceeded
Code
#include<bits/stdc++.h>
using namespace std;
/*
* @author abhishek
*
*/
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int tt;
cin>>tt;
while(tt--) {
int n;
cin>>n;
string s, p;
cin>>s>>p;
if(s==p) {
cout<<"YES"<<endl;
continue;
}
if(n==1) {
cout<<"NO"<<endl;
continue;
}
auto toMask= [&](string &x) {
int m= 0;
for(char c : x) {
m= (m<<1)| (c=='1');
}
return m;
};
unordered_set<int > visited;
queue<int >q;
int start= toMask(s);
int target= toMask(p);
q.push(start);
visited.insert(start);
bool f= false;
while(!q.empty()) {
int curr= q.front();
q.pop();
for(int i=0;i<n-1;i++) {
for(int j=0;j<=1;j++ ) {
int next= curr;
next&= ~(1<<(n-i-1));
next&= ~(1<<(n-i-2));
if(j==0) {
next|= (1<<(n-i-2));
} else{
next|= (1<<(n-i-1));
}
if(!visited.count(next)) {
if(next==target) {
f= true;
break;
}
visited.insert(next);
q.push(next);
}
}
if(f) {
break;
}
}
if(f) {
break;
}
}
if(f) {
cout<<"YES"<<endl;
} else{
cout<<"NO"<<endl;
}
}
return 0;
}
Information
- Submit By
- Type
- Submission
- Problem
- P1233 B. Make Binary Strings Equal
- Contest
- Happy New Year 2026
- Language
- C++17 (G++ 13.2.0)
- Submit At
- 2026-01-06 15:34:41
- Judged At
- 2026-01-06 15:34:41
- Judged By
- Score
- 5
- Total Time
- ≥603ms
- Peak Memory
- ≥40.703 MiB