题意 : 给你两个只包含 0 和 1 的字符串 a, b,定义函数 f ( A, B ) 为 字符串A和字符串B 比较

存在多少个位置 i 使得 A[ i ] != B[ i ] ,例如

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

问你 取出 a 中 所有 长度 为 lenb (字符串b的长度) 的子串 c, 求 f ( c, b) 为偶数的 c 的个数。

解 : 显然, a 中 存在  lena - lenb + 1 个 c, 直接枚举显然是爆的, 字符串只包含 0 1 且 题目只问 f ( b, c ) 的奇偶性。

想到异或,若 在位置 i 上 b[ i ] != c[ i ], 则 b[ i ] ^ c[ i ]  = 1; 否则  b[ i ] ^ c[ i ]  = 0;

所以可以 用 一个 ans 来记录  f ( b, c) 的奇偶性, 那么只要枚举 b字符串的长度,然后 ans = ans ^ b[ i ] ^ c[ i ] 就行了

最后判断一下 ans 的奇偶性看满不满足就行了。

这题的关键是 a  的 长度为 lenb 的子串 c 有很多, 你不可能对于每个 c 都去遍历一遍 b字符串。

首先,枚举a 的所有长度为 lenb 的子串 c  枚举 i , 字符串 c 就是 a[ i ] ~ a[ i + lenb - 1];

首先,先取第一个 c ;  a[ 0 ] ~ a[ lenb - 1] 与 b 进行比较 求出 ans0;

然后 对于 第二个 c : a[ 1 ] ~ a[ lenb ] 的 ans1   就会等于 ans0 ^ a[ 0 ] ^ a[ lenb ];

同理 对于 第 i 个 c:  a[ i ] ~ a[ i + lenb - 1 ] 的 ans( i ) = ans( i - 1 ) ^ a[ i - 1 ] ^ a[ i + lenb - 1]

为什么可以这样写呢; 那就是 因为异或的性质啦。  异或 a [ i - 1 ] 是消除 a[ i - 1 ] 的影响

异或 a [ i + lenb - 1] 是加入计算;

举个例: 现在令  a = 01100010  ;  b = 00110;

第一个 c 的 ans 是

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

第二个 c 的 ans

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

=  0 ^ ( 0 ^ 0 )^( 1 ^ 0 )^( 1 ^ 1 )^( 0 ^ 1 )^( 0 ^ 0 )^ 0  // 异或两次相当于没有异或

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

用到了 异或 运算  的 交换律 和  异或两次等于没异或的性质。   挺巧妙的这个思维。

代码里 的 i 和我说的 i 不一样,不过道理都是一样的啦;

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#define LL long long
#define ULL unsigned long long
#define rep(i,j,k) for(int i=j;i<=k;i++)
#define dep(i,j,k) for(int i=k;i>=j;i--)
#define INF 0x3f3f3f3f
#define mem(i,j) memset(i,j,sizeof(i))
#define make(i,j) make_pair(i,j)
#define pb push_back
#define Pi acos(-1.0)
using namespace std;
const int N = ;
int main() {
string a, b;
cin >> a >> b;
int ans = , coun = ;
int lena = a.size(); int lenb = b.size();
rep(i, , lenb - ) ans = ans ^ ( a[i] - '') ^ (b[i] - '');
if(ans % == ) coun++;
rep(i, lenb, lena - ) {
ans = ans ^ (a[i - lenb] - '') ^ (a[i] - '');
if(ans % == ) coun++;
}
cout << coun << endl;
return ;
}

C Vus the Cossack and Strings ( 异或 思维)的更多相关文章

  1. CodeForces - 1186 C. Vus the Cossack and Strings (异或)

    Vus the Cossack has two binary strings, that is, strings that consist only of "0" and &quo ...

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

    C. Vus the Cossack and Strings Vus the Cossack has two binary strings, that is, strings that consist ...

  3. codeforces 1186C Vus the Cossack and Strings

    题目链接:https://codeforc.es/contest/1186/problem/C 题目大意:xxxxx(自认为讲不清.for instance) 例如:a="01100010& ...

  4. Codeforces F. Vus the Cossack and Numbers(贪心)

    题目描述: D. Vus the Cossack and Numbers Vus the Cossack has nn real numbers aiai. It is known that the ...

  5. E. Vus the Cossack and a Field (求一有规律矩形区域值) (有一结论待证)

    E. Vus the Cossack and a Field (求一有规律矩形区域值) 题意:给出一个原01矩阵,它按照以下规则拓展:向右和下拓展一个相同大小的 0 1 分别和原矩阵对应位置相反的矩阵 ...

  6. 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 ...

  7. 题解【Codeforces1186A】 Vus the Cossack and a Contest

    这题是入门难度的题目吧-- 根据题意可以得出,只有当\(m\)和\(k\)都大于等于\(n\)时,\(Vus\)才可以实现他的计划. 因此,我们不难得出以下\(AC\)代码: #include < ...

  8. 『Codeforces 1186E 』Vus the Cossack and a Field (性质+大力讨论)

    Description 给出一个$n\times m$的$01$矩阵$A$. 记矩阵$X$每一个元素取反以后的矩阵为$X'$,(每一个cell 都01倒置) 定义对$n \times m$的矩阵$A$ ...

  9. Codeforces 1186F - Vus the Cossack and a Graph 模拟乱搞/欧拉回路

    题意:给你一张无向图,要求对这张图进行删边操作,要求删边之后的图的总边数 >= ceil((n + m) / 2), 每个点的度数 >= ceil(deg[i] / 2).(deg[i]是 ...

随机推荐

  1. url编码问题小计

    昨天通过get访问服务器遇到了服务器获取不到参数的问题,最后排查下来是因为url编码的原因,之前使用的是字符串拼接,所以有一些特殊字符如‘%’没有正确的编码, 通过改成各个部分编码,正确获取到数据. ...

  2. 并不对劲的CF1237D&E:Balanced Playlist and Binary Search Trees

    CF1237D Balanced Playlist 题意 有一个长度为\(n\)(\(n\leq 10^5\))的循环播放歌单,每首歌有一个优秀值\(a_i\)(\(a_i\leq 10^9\)). ...

  3. Java EE javax.servlet.http中的HttpSession接口

    HttpSession接口 public interface HttpSession (https://docs.oracle.com/javaee/7/api/javax/servlet/http/ ...

  4. 阿里云语音合成(汉语英语)带UI界面的小程序(python)

    一,项目说明 将汉文转汉语.英文转英语,同时又有逗号<###English###>,<,,,>和句号<...>标志符用于文件处理.其中英文包含在### 英文 ### ...

  5. O051、Create Volume 操作 (Part II)

    参考https://www.cnblogs.com/CloudMan6/p/5612147.html       1.cinder-scheduler 也会启动一个工作流 volume_create_ ...

  6. Python算法题(一)——青蛙跳台阶

    题目一(青蛙跳台阶): 一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法. 分析: 假设只有一级台阶,则总共只有一种跳法: 假设有两级台阶,则总共有两种跳法: ...

  7. 9 java 笔记

    1 所有的java对象都可以和字符串进行连接运算 2 在java中判断两个变量是否相等时:==运算符适用于基本类型的判断 3 JVM常量池保证相同的字符串直接量只有一个,不会产生多个相同的副本 4 O ...

  8. Java学习笔记【一、环境搭建】

    今天把java的学习重新拾起来,一方面是因为公司的项目需要用到大数据方面的东西,需要用java做语言 另一方面是原先使用的C#公司也在慢慢替换为java,为了以后路宽一些吧,技多不压身 此次的学习目标 ...

  9. kubernetes资源清单之pod

    什么是pod? Pod是一组一个或多个容器(例如Docker容器),具有共享的存储/网络,以及有关如何运行这些容器的规范. Pod的内容始终位于同一地点,并在同一时间安排,并在共享上下文中运行. Po ...

  10. deepin禁用笔记本自带键盘

    参考命令: sudo apt install xinput xinput xinput list-props 'AT Translated Set 2 keyboard' xinput set-pro ...