https://codeforces.com/contest/1504/problem/B
思路:后往前枚举前缀,打标记代替修改即可。
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=3e5+1000;
typedef int LL;
inline LL read(){LL x=0,f=1;char ch=getchar(); while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
char s1[maxn],s2[maxn];
LL sum0[maxn],sum1[maxn];
int main(void){
cin.tie(0);std::ios::sync_with_stdio(false);
LL t;cin>>t;
while(t--){
LL n;cin>>n;
for(LL i=0;i<=n+10;i++){
sum0[i]=sum1[i]=0;
}
// for(LL i=1;i<=n;i++){
// cout<<sum0[i]<<" ";
// }
// cout<<"\n";
// for(LL i=1;i<=n;i++){
// cout<<sum1[i]<<" ";
// }
// cout<<"\n";
cin>>(s1+1);
cin>>(s2+1);
for(LL i=1;i<=n;i++){
sum0[i]=sum0[i-1]+(s1[i]=='0');
sum1[i]=sum1[i-1]+(s1[i]=='1');
}
LL lazy=1;
bool flag=1;
for(LL i=n;i>=1;i--){
if(lazy==1){
if(s1[i]==s2[i]) continue;
else{
if(sum0[i]!=sum1[i]){
flag=0;
break;
}
lazy=0;
}
}
else if(lazy==0){
if(s1[i]!=s2[i]) continue;
else{
if(sum0[i]!=sum1[i]){
flag=0;
break;
}
lazy=1;
}
}
}
if(flag){
cout<<"YES"<<"\n";
}
else{
cout<<"NO"<<"\n";
}
}
return 0;
}