C. Vus the Cossack and Strings

 

 题面:

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| \leq |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.

 /******** 

 http://codeforces.com/contest/1186/problem/C
题意:给2个0,1串,a串比b串长。 问a串有多少个和b串等长且不同的字符个数为偶数的串。 思路:首先比较出a中第一个串(记为a0)和b的不同字符个数(记为ans0)。然后a中第二个串(记为a1)将不在与b比较而是与a中第一个串比较。 这里分2种情况: (1):a1[i]==a0[i]时不用管;(因为此情况时,无论a0[i]与b[i]是否相同,a1[i]与b[i]的情况都与前者一样) (2):a1[i]!=a0[i]时记录这种情况个数(记为s1)。(a1[i]与b[i]的情况恰好与a0[i]与b[i]的情况相反) 如果(ans0+s1)%2==0该串就是答案之一; 那么对于任意一个a的与b大小相等的串到底是不是答案,我们都可以通过(前一个串的不同字符个数+该串中(2)情况的个数)来判断。 而对于任意一个串, (2)情况的个数,都可以通过预处理0(1)出来。 ****/ #include<bits/stdc++.h>

using namespace std; int s[1000009],ans[1000009]; int main() { string sa,sb; int la,lb; cin>>sa>>sb; la=sa.size( ),lb=sb.size(); s[0]=0; for(int i=1;i<la;i++) { if(sa[i]==sa[i-1])s[i]=s[i-1]; else s[i]=s[i-1]+1; } int k=0; for(int i=0;i<lb;i++) { if(sa[i]!=sb[i])ans[lb-1]++; } if(ans[lb-1]%2==0)ans[lb-1]=0; for(int i=lb;i<la;i++) { ans[i]=(ans[i-1]+s[i]-s[i-lb])%2; } int tans=0; for(int i=lb-1;i<la;i++) { if(ans[i]==0) tans++; } cout<<tans<<endl; return 0; }

 

posted @ 2019-06-30 19:41  一只球球  阅读(339)  评论(0编辑  收藏  举报