C. Vus the Cossack and Strings
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Vus the Cossack has two binary strings, that is, strings that consist only of “0” and “1”. We call these strings a and b. It is known that |b|≤|a|, that is, the length of b is at most the length of a.
The Cossack considers every substring of length |b| in string a. Let’s call this substring c. He matches the corresponding characters in b and c, after which he counts the number of positions where the two strings are different. We call this function f(b,c).
For example, let b=00110, and c=11000. In these strings, the first, second, third and fourth positions are different.
Vus the Cossack counts the number of such substrings c such that f(b,c) is even.
For example, let a=01100010 and b=00110. a has four substrings of the length |b|: 01100, 11000, 10001, 00010.
f(00110,01100)=2;
f(00110,11000)=4;
f(00110,10001)=4;
f(00110,00010)=1.
Since in three substrings, f(b,c) is even, the answer is 3.
Vus can not find the answer for big strings. That is why he is asking you to help him.
Input
The first line contains a binary string a (1≤|a|≤106) — the first string.
The second line contains a binary string b (1≤|b|≤|a|) — the second string.
Output
Print one number — the answer.
Examples
inputCopy
01100010
00110
outputCopy
3
inputCopy
1010111110
0110
outputCopy
4
Note
The first example is explained in the legend.
In the second example, there are five substrings that satisfy us: 1010, 0101, 1111, 1111.
看每一段c中1的个数对2取模是否和b中1的个数对2取模一样。用前缀和记录a的每一段中1的个数。
#include<iostream>
#include<string.h>
#include<algorithm>
#include<queue>
#include<map>
#include<cmath>
#include<vector>
#include<set>
#define LL long long
#define mem(a, b) memset(a, b, sizeof(a))
#define INF 0x3f3f3f3f
#define N 1000001
#define MOD
using namespace std;
char a[N], b[N];
int cntc, cntb, pre[N];
int main() {
while(scanf("%s",a)!=EOF) {
scanf("%s",b);
int lena=strlen(a), lenb=strlen(b);
for(int i=0; i<lenb; i++){
if(b[i]=='1') cntb++;
}
for(int i=1; i<=lena; i++) {//前缀和
if(a[i-1]=='1')
pre[i]=pre[i-1]+1;
else
pre[i]=pre[i-1];
}
int ans=0;
for(int i=0; i+lenb<=lena; i++){
if((pre[i+lenb]-pre[i])%2==cntb%2)
ans++;
}
cout<<ans<<endl;
}
}