直接上代码:
#include<iostream>
#include<cstring>
using namespace std;
int main(){
int t,n,h;
string s1,s2;
string t1,t2;
int ans;
cin>>t;
for(int i=0;i<t;i++){
cin>>n>>s1>>s2>>t1>>t2;
ans=0;
for(int j=0;j<n;j++){
if(s1[j]!=s2[j]){
for(h=j+1;h<n;h++){
if(s1[h]!=s2[h]&&s1[j]!=s1[h]){
if(t1[j]=='1'&&t1[h]=='1'){
swap(s1[j],s1[h]);
break;
}else if(t2[j]=='1'&&t2[h]=='1'){
swap(s2[j],s2[h]);
break;
}
}
}
}
}
for(int j=0;j<n;j++){
if(s1[j]==s2[j]){
ans++;
}
}
cout<<ans<<endl;
}
return 0;
}
时间复杂度也不知道怎么分析反正超过O(n)小于O(n^2),代码不多,但是成一坨了(代码整体看着像金字塔)