样例都能过,官方数据 10 分。
#include <iostream>
#include <string>
#include <cstdio>
using namespace std;
int T, n, hd1, hd2, cntZer1[100010], cntZer2[100010], cntOne1[100010], cntOne2[100010], ans;
string str1, str2, typ1, typ2;
int main() {
freopen("edit.in", "r", stdin);
freopen("edit.out", "w", stdout);
cin >> T;
while(T--) {
cin >> n;
cin >> str1 >> str2 >> typ1 >> typ2;
ans = 0;
hd1 = hd2 = n;
cntZer1[n] = cntOne1[n] = 0;
for(int i = 0; i < n; i++) {
if(typ1[i] == '0') {
cntZer1[i] = cntOne1[i] = 0;
hd1 = i;
} else {
cntZer1[hd1] += (str1[i] == '0');
cntOne1[hd1] += (str1[i] == '1');
}
if(typ2[i] == '0') {
cntZer2[i] = cntOne2[i] = 0;
hd2 = i;
} else {
cntZer2[hd2] += (str2[i] == '0');
cntOne2[hd2] += (str2[i] == '1');
}
}
hd1 = hd2 = n;
for(int i = 0; i < n; i++) {
if(typ1[i] == '0' && typ2[i] == '0') {
ans += (str1[i] == str2[i]);
hd1 = hd2 = i;
} else if(typ1[i] == '0') {
hd1 = i;
if(str1[i] == '0' && cntZer2[hd2]) {
cntZer2[hd2]--;
ans++;
}
if(str1[i] == '1' && cntOne2[hd2]) {
cntOne2[hd2]--;
ans++;
}
} else if(typ2[i] == '0') {
hd2 = i;
if(str2[i] == '0' && cntZer1[hd1]) {
cntZer1[hd1]--;
ans++;
}
if(str2[i] == '1' && cntOne1[hd1]) {
cntOne1[hd1]--;
ans++;
}
}
}
hd1 = hd2 = n;
for(int i = 0; i < n; i++) {
if(typ1[i] == '0') {
hd1 = i;
}
if(typ2[i] == '0') {
hd2 = i;
}
if(typ1[i] == '1' && typ2[i] == '1') {
if(cntOne1[hd1] && cntOne2[hd2]) {
cntOne1[hd1]--;
cntOne2[hd2]--;
ans++;
} else if(cntZer1[hd1] && cntZer2[hd2]) {
cntZer1[hd1]--;
cntZer2[hd2]--;
ans++;
}
}
}
cout << ans << endl;
}
return 0;
}