题目传送门:http://codeforces.com/contest/1017/problem/B
本题可以找规律,因为是or运算,所以第二个01串上的1不用管,因为无论怎么调换第一个01串,1所对应的那位结果永远是1,所以只用看第二个01串的0位。比如样例,第一个是01011,第二个是11001,只用看第3,4位即可。第3位的0对应的是0,所以在第一个字符串找1,而1有两种,一种是0对应的1,还有一种是1对应的1。为甚么要分两种呢?因为0对应的1可能会被重复计算,所以我从后面计算后缀,也就是出现的0对应的0,0对应的1。还有一种是1对应的0和1对应的1,折中不用担心被重复计算,每次计算都需要加上。
在解释一下,上文中0对应的1是指str2的0位对应的str1的1位。
注意,最后的结果要用long long ,int 会WA,已经犯了好几次这样的错误了。呜呜呜
废话少说,具体看AC代码:
这里写代码片
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std;
typedef unsigned long long ULL;
const int maxn = 1e5 + 100;
string str1,str2;
int sum1[maxn],sum2[maxn];
int main(){
int n = 0;
cin >> n;
cin >> str1 ;
cin >> str2;
memset(sum1,0,sizeof(sum1));
memset(sum2,0,sizeof(sum2));
int cnt1 = 0,cnt2 = 0,cnt3 = 0,cnt4 = 0;
for(int i = n - 1;i >= 0;--i){
if(str1[i] == '0' && str2[i] == '0' ){
sum1[i] = cnt1;
}
if(str1[i] == '1' && str2[i] == '0' ){
sum2[i] = cnt3;
}
if(str2[i] == '0' && str1[i] == '1'){
++cnt1;
}
if(str2[i] == '1' && str1[i] == '1'){
++cnt2;
}
if(str2[i] == '0' && str1[i] == '0'){
++cnt3;
}
if(str2[i] == '1' && str1[i] == '0'){
++cnt4;
}
}
ULL ans = 0;
for(int i = 0;i < n;++i){
if(str1[i] == '0' && str2[i] == '0'){
ans += sum1[i] + cnt2;
}
if(str1[i] == '1' && str2[i] == '0'){
ans += sum2[i] + cnt4;
}
}
cout << ans << endl;
return 0;
}