Problem - 1186C - Codeforces
题目大意:给定两个仅由 0 0 0和 1 1 1组成的字符串 a , b a,b a,b.定义 c c c为 b b b的子串,定义函数 f ( b , c ) f(b, c) f(b,c)的值为 b , c b,c b,c两串的不相同的位数.求 a a a有多少个子串 c c c满足 f ( b , c ) f(b, c) f(b,c)为偶数.
解题思路:观察一下还是很好猜的,当两个串中的 0 , 1 0,1 0,1的个数之差为偶数时,那么每有一个 b b b中的 0 0 0和 c c c中的 1 1 1不匹配时,与之对应的一定有一个 b b b中的1和 c c c中的 0 0 0不匹配,永远是成偶数的.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define syncfalse ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
const int N = 1e6+5;
char a[N], b[N];
int pre[N][2];
int main(){
syncfalse
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
cin>>a+1>>b+1;
int n = strlen(a+1), m = strlen(b+1);
int num0 = 0, num1 = 0;
for (int i = 1; i <= m; ++i){
if (b[i]=='0')num0++;
else num1++;
}
for (int i = 1; i <= n; ++i){
pre[i][0]+=pre[i-1][0];
pre[i][1]+=pre[i-1][1];
if (a[i]=='1')pre[i][1]++;
else pre[i][0]++;
}
int l = 1;
int ans = 0;
for (int r = m; r <= n; ++r, ++l){
int now1 = pre[r][1]-pre[l-1][1];
int tem = abs(now1-num1);
if (tem%2==0)ans++;
}
cout << ans << "\n";
return 0;
}