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 aa and bb. It is known that |b|≤|a||b|≤|a|, that is, the length of bb is at most the length of aa.

The Cossack considers every substring of length |b||b| in string aa. Let's call this substring cc. He matches the corresponding characters in bband cc, after which he counts the number of positions where the two strings are different. We call this function f(b,c)f(b,c).

For example, let b=00110b=00110, and c=11000c=11000. In these strings, the first, second, third and fourth positions are different.

Vus the Cossack counts the number of such substrings cc such that f(b,c)f(b,c) is even.

For example, let a=01100010a=01100010 and b=00110b=00110. aa has four substrings of the length |b||b|: 0110001100, 1100011000, 1000110001, 0001000010.

  • f(00110,01100)=2f(00110,01100)=2;
  • f(00110,11000)=4f(00110,11000)=4;
  • f(00110,10001)=4f(00110,10001)=4;
  • f(00110,00010)=1f(00110,00010)=1.

Since in three substrings, f(b,c)f(b,c) is even, the answer is 33.

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 aa (1≤|a|≤1061≤|a|≤106) — the first string.

The second line contains a binary string bb (1≤|b|≤|a|1≤|b|≤|a|) — the second string.

Output

Print one number — the answer.

Examples
input

Copy
01100010
00110
output

Copy
3
input

Copy
1010111110
0110
output

Copy
4
Note

The first example is explained in the legend.

In the second example, there are five substrings that satisfy us: 10101010, 01010101, 11111111, 11111111.

题意:题目给了一个较长字符串a以及一个较短字符串b,在a中取与b长度相等的子串,比较子串与b字符串中有几组字符不一样。

当天下午由于在探讨研究这场CF B题的4 4 情况,而导致后来没怎么看这道题,但实际上这道题对于位运算的运用是十分的基础而且有效的。

顺带一提,B题4 4的情况,不仅hack掉了基本所有的AC代码,甚至让这场比赛都不算rate了

先科普一下 位运算中的 异或(运算符"^"),在二进制中,如果两个数字相同的话,那么异或的结果就是0,否则就是1;同时两次异或同一个数字,就相当于没有没有异或过这个数字

先贴一下代码

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = ;
char a[maxn],b[maxn];
int ans = ,sum = ;
int main(){
scanf("%s",a);
scanf("%s",b);
int len_a = strlen(a);
int len_b = strlen(b);
for(int i = ; i < len_b ; i++){
ans=ans^a[i]^b[i];
}
if(ans% == ) sum++;
for(int i = len_b ; i < len_a ; i++){
ans = ans^a[i-len_b]^a[i];
if(ans % == ) sum++;
}
printf("%d\n",sum);
return ;
}

C

例如,样例一:

一开始先对于 i = 0  到 i = strlen(b) 中 b 字符串和a的子串进行异或操作,如果异或结果(ans)可以整除2,那么就代表这个子串对于b而言有偶数组不同。

之后就可以有i = len 向 i = strlen(a)中逐位挪移,而计算结果就是  新的 ans  =  旧的 ans ^ a[i - len] ^ a[i])。^a[i - len]就是消除a[i - len]的影响,^a[i]就是将a[i]的影响加入计算

一开始的状态

(0^0)^(1^0)^(1^1)^(0^1)^(0^0)

向右挪移一位

(0^0)^(1^0)^(1^1)^(0^1)^(0^0)^0^0   // 第一个0 是a[i - len],第二个0是a[i];

= 0^(0^0)^(1^0)^(1^1)^(0^1)^(0^0)^0  // 因为一个数字异或两次相当于没有异或

= ·(0^1)^(0^1)^(1^0)^(1^0)^(0^0)

这样只需要O(n)的时间复杂度就可以完成运算。

一个从很久以前就开始做的梦。

Vus the Cossack and Strings(Codeforces Round #571 (Div. 2))(大佬的位运算实在是太强了!)的更多相关文章

  1. Codeforces Round #461 (Div. 2)B-Magic Forest+位运算或优雅的暴力

    Magic Forest 题意:就是在1 ~ n中找三个值,满足三角形的要求,同时三个数的异或运算还要为0: , where  denotes the bitwise xor of integers  ...

  2. Codeforces Round #571 (Div. 2)

    A. Vus the Cossack and a Contest 签. #include <bits/stdc++.h> using namespace std; int main() { ...

  3. Codeforces Round #571 (Div. 2)-D. Vus the Cossack and Numbers

    Vus the Cossack has nn real numbers aiai. It is known that the sum of all numbers is equal to 00. He ...

  4. codeforces 559b//Equivalent Strings// Codeforces Round #313(Div. 1)

    题意:定义了字符串的相等,问两串是否相等. 卡了时间,空间,不能新建字符串,否则会卡. #pragma comment(linker,"/STACK:1024000000,102400000 ...

  5. Codeforces Round #344 (Div. 2)(按位或运算)

    Blake is a CEO of a large company called "Blake Technologies". He loves his company very m ...

  6. Codeforces Round #533 (Div. 2)题解

    link orz olinr AK Codeforces Round #533 (Div. 2) 中文水平和英文水平都太渣..翻译不准确见谅 T1.给定n<=1000个整数,你需要钦定一个值t, ...

  7. 水题 Codeforces Round #302 (Div. 2) A Set of Strings

    题目传送门 /* 题意:一个字符串分割成k段,每段开头字母不相同 水题:记录每个字母出现的次数,每一次分割把首字母的次数降为0,最后一段直接全部输出 */ #include <cstdio> ...

  8. Codeforces Round #177 (Div. 1) 题解【ABCD】

    Codeforces Round #177 (Div. 1) A. Polo the Penguin and Strings 题意 让你构造一个长度为n的串,且里面恰好包含k个不同字符,让你构造的字符 ...

  9. Codeforces Round #112 (Div. 2)

    Codeforces Round #112 (Div. 2) C. Another Problem on Strings 题意 给一个01字符串,求包含\(k\)个1的子串个数. 思路 统计字符1的位 ...

随机推荐

  1. ZOJ 1409 communication system 双变量型的DP

    这个题目一开始不知道如何下手,感觉很像背包,里面有两个变量,一个带宽B,一个价格P,有n个设备,每个设备有k个可选的器材(只需选一个),每个器材都有自己的B和P, n个设备选n个器材,最终,FB=所有 ...

  2. Storm 流式计算框架

    1. 简介 是一个分布式, 高容错的 实时计算框架 Storm进程常驻内存, 永久运行 Storm数据不经过磁盘, 在内存中流转, 通过网络直接发送给下游 流式处理(streaming) 与 批处理( ...

  3. 个人vim简单配置

    精简.vimrc配置,简约不简单 该配置没有花里胡哨的插件,只是用ctags和cscope然后配合vim提供的基础功能就可以完成常见的代码编辑,浏览,查找等工作. "************ ...

  4. Vue 改变数组触发视图更新

    最近给table做了一个点击排序的功能,数组更改以后发现data数据变了,但是视图不更新 写惯了js的我们随手一串代码 this.items[2]={message:"Change Test ...

  5. js库链接

    1.autoHeightTextarea自适应高度的textarea是一款jquery插件,支持链式调用,支持设置最小行数.最小高度.最大行数和最大高度,在输入文字的时候实现textarea的高度自适 ...

  6. POJ 3071:Football

    Football Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3600   Accepted: 1844 Descript ...

  7. CSS - 导入Google Web 字体

    @import url('http://fonts.googleapis.com/css?family=Poppins:100,200,300,400,500,600,700,800,900'); b ...

  8. Maven:java.lang.RuntimeException: Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty

    直接参考前面一篇文章中间介绍的第2种方法即可:Maven:sun.security.validator.ValidatorException: PKIX path building failed: s ...

  9. 单变量线性回归(Linear Regression with One Variable)与代价函数

    所谓的单变量线性回归问题就是监督学习的一部分. 通过构建数学模型给出一个相对准确的数值,也就是预测模型,通过将数据通过数学模型,衍生至回归问题 通过以下的几个例子,我们来研究单变量线性回归. 1.王阿 ...

  10. XML--XML Schema Definition(三)

    参考 http://www.w3school.com.cn/schema/index.asp XSD 复合元素 复合元素指包含其他元素及/或属性的 XML 元素. 有四种类型的复合元素: 空元素 包含 ...