Codeforces #571 C Vus the Cossack and Strings

探讨了在给定长度不等的二进制字符串中,寻找特定长度子串与其匹配,统计匹配差异为偶数的子串数量的算法。通过分析字符移动对答案的影响,提出了解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接


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.


Solution 1

这是我在考场上想出来的做法,但是写挂了

考虑串b移动之后会发生什么
例如
00110 ->
*00110
可以发现对于没移动的之前的第 i i i位,如果 i − 1 i-1 i1位和 i i i位相同,那么移动后在 i i i这个位置上对答案的贡献是不会变的。
那么考虑不相同的时候假设第 i i i位是 0 0 0现在匹配到 1 1 1 i − 1 i-1 i1位是 1 1 1那么移动后在 i i i这个位置上对答案的贡献是 − 1 -1 1
如果第 i i i位匹配到 0 0 0,那么移动后对答案的贡献就是 + 1 +1 +1
如果第 i i i 1 1 1情况类似。
也就是如果 b b b i i i位和 i − 1 i-1 i1位不一样,每移动一次对答案就会有一个^1的贡献(答案只考虑奇偶)
对b串求一次这个

int trans = 0;
for(int i=1;i<n;i++){
	trans ^= (str[i]-'0')^(str[i+1]-'0');
}

在这里插入图片描述
但是 b b b串移动之后,答案异或上b串的 t r a n s trans trans后只是更新了蓝色部分对答案的贡献。
还需要手动更新红色和绿色。

感觉我说的有点乱,可以看代码理解一下。
ps:b串的 t r a n s trans trans就是第一个异或最后一个,我也不知道为什么这么神奇。

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e6;
char s1[MAXN],s2[MAXN];
int n,m;
int main(){
	scanf("%s",s1+1);
	scanf("%s",s2+1);
	n=strlen(s1+1);
	m=strlen(s2+1);
	int flag = 0;
	flag ^= s2[1] ^ s2[m];
	int ans = 0;
	int tmp = 0;
	for(int i=1;i<=m;i++){
		if(s1[i] != s2[i])
			tmp ^= 1;
	}
	if(!tmp) ans ++;
	int point = m;
	while(point < n){
		point ++;
		tmp ^= flag;
		if(s1[point-m]!=s2[1])
			tmp ^= 1;
		if(s1[point] != s2[m]){
			tmp ^= 1;
		}
		if(!tmp){
			ans ++;
		}
	}
	cout<<ans;
	return 0;
}

Solution 2

我在结束之后 O r z Orz Orz了一下其他人的代码
发现了这个做法
考虑两个等长的 0 / 1 0/1 0/1 a , b a,b a,b a a a中有 s a s_a sa 1 1 1, b b b中有 s b s_b sb 1 1 1
那么这两个串不相同的位置的个数的就行就是 s a + s b s_a+s_b sa+sb的奇偶性
为什么?
先考虑只有 0 0 0 1 1 1匹配和 0 0 0 0 0 0匹配,显然正确,并且不同的个数就是 s a + s b s_a+s_b sa+sb
因为如果有多个 1 1 1匹配 1 1 1,那么不同的个数就是 s a + s b − k ∗ 2 s_a+s_b-k*2 sa+sbk2减掉的数是 2 2 2的倍数,不改变奇偶性。
看完之后感觉自己是个zz


Solution 3

这不是个fft板子题吗

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值