CodeForces - 1186C
题意
给出两个01字符串s,t。
两个长度相等的字符串b,c。
f(b,c)为b和c相同位置上字符不相同的个数。
比如:
00110
11000
他们第1 2 3 4 位置上的字符不相同,那么f(b,c)=4。
让求出f(t,x)为偶数的x的个数(x为长度和t相等的s的子串)
思路
f(b,c)为偶数当且仅当b,c中1的个数相加为偶数。
证明:
假如b中有x个1,c中有y个1。
只有0和1相对应的时候才不相同。
假设此时有z个b中的1和z个c中的1是对应的。
此时还有x+y-2*z
个10相对应。(即不相同的个数)
只有当x+y为偶数的时候x+y-2*z才是偶数。
代码
//#include<bits/stdc++.h>
#include<math.h>
#include<time.h>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<queue>
#include<set>
#include<stack>
#include<map>
#include<string>
#include<iostream>
#define pb push_back
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=1e6+10;
const int mod=1e9+7;
const int inf=0x3f3f3f3f;
char s[N],t[N];
int pre[N];
int main()
{
scanf("%s%s",s+1,t+1);
int lens=strlen(s+1);
int lent=strlen(t+1);
for(int i=1;i<=lens;i++)
pre[i]=pre[i-1]+(s[i]=='1');
int num=0;
for(int i=1;i<=lent;i++)
num+=(t[i]=='1');
int ans=0;
for(int i=lent;i<=lens;i++)
{
if((pre[i]-pre[i-lent]+num)%2==0)
ans++;
}
printf("%d\n",ans);
return 0;
}