20分调错
查看原帖
20分调错
511268
PIKA_PIKA楼主2024/12/6 20:00
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int T,n,t[N][2],ts[N],h[N][2],hs[N],ans;
char a[2][N],b[2][N];
int main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	freopen("edit.in","r",stdin);
	freopen("edit.out","w",stdout);
	cin>>T;
	while(T--){
		cin>>n;ans=0;
		memset(t,0,sizeof t);
		memset(h,0,sizeof h);
		memset(ts,0,sizeof ts);
		memset(hs,0,sizeof hs);
		cin>>a[0]+1;
		cin>>a[1]+1;
		cin>>b[0]+1;
		cin>>b[1]+1;
		int cnt=1;
		for(int i=1;i<=n;i++){
			if(b[0][i]=='0'){
				ts[i]=cnt;cnt++;
			}
			else t[cnt][a[0][i]-'0']++;
		}
		if(b[0][n]!='0') ts[n]=cnt;
		cnt=1;
		for(int i=1;i<=n;i++){
			if(b[1][i]=='0'){
				hs[i]=cnt;cnt++;
			}
			else h[cnt][a[1][i]-'0']++;
		}
		if(b[1][n]!='0') hs[n]=cnt;
		int p1=1,p2=1;
		for(int i=1;i<=n;i++){
			if(b[0][i]=='0'&&b[1][i]=='0'){
				ans+=min(t[p1][0],h[p2][0]);
				ans+=min(t[p1][1],h[p2][1]);
				if(a[0][i]==a[1][i]) ans++;
				p1++;p2++;continue;
			}
			if(ts[i]){
				t[p1][a[0][i]-'0']++;
				ans+=min(t[p1][0],h[p2][0]);
				ans+=min(t[p1][1],h[p2][1]);
				if(h[p2][0]>=t[p1][0]&&h[p2][1]>=t[p1][1]){
					h[p2][0]-=t[p1][0];
					h[p2][1]-=t[p1][1];
				}
				else if(h[p2][0]<t[p1][0]){
					h[p2][1]-=t[p1][0]-h[p2][0];
					h[p2][1]-=t[p1][1];
					h[p2][0]=0;
				}
				else{
					h[p2][0]-=t[p1][1]-h[p2][1];
					h[p2][0]-=t[p1][0];
					h[p2][1]=0;
				}
				if(hs[i]) p2++;
				p1++;
			}
			else if(hs[i]){
				h[p2][a[1][i]-'0']++;
				ans+=min(t[p1][0],h[p2][0]);
				ans+=min(t[p1][1],h[p2][1]);
				if(t[p1][0]>=h[p2][0]&&t[p1][1]>=h[p2][1]){
					t[p1][0]-=h[p2][0];
					t[p1][1]-=h[p2][1];
				}
				else if(h[p2][0]>t[p1][0]){
					t[p1][1]-=h[p2][0]-t[p1][0];
					t[p1][1]-=h[p2][1];
					t[p1][0]=0;
				}
				else{
					t[p1][0]-=h[p2][1]-t[p1][1];
					t[p1][0]-=h[p2][0];
					t[p1][1]=0;
				}
				p2++;
			}
		}
		cout<<ans<<"\n";
	}
	return 0;
}
2024/12/6 20:00
加载中...