【Codeforces Round 336 (Div 2) B】【水题 前缀和】Hamming Distance Sum 双字符串所有位置匹配下的差异数之和

原创 2015年12月25日 11:46:32
B. Hamming Distance Sum
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Genos needs your help. He was asked to solve the following programming problem by Saitama:

The length of some string s is denoted |s|. The Hamming distance between two strings s and t of equal length is defined as , where si is the i-th character of s and ti is the i-th character of t. For example, the Hamming distance between string "0011" and string "0110" is |0 - 0| + |0 - 1| + |1 - 1| + |1 - 0| = 0 + 1 + 0 + 1 = 2.

Given two binary strings a and b, find the sum of the Hamming distances between a and all contiguous substrings of b of length |a|.

Input

The first line of the input contains binary string a (1 ≤ |a| ≤ 200 000).

The second line of the input contains binary string b (|a| ≤ |b| ≤ 200 000).

Both strings are guaranteed to consist of characters '0' and '1' only.

Output

Print a single integer — the sum of Hamming distances between a and all contiguous substrings of b of length |a|.

Sample test(s)
input
01
00111
output
3
input
0011
0110
output
2
Note

For the first sample case, there are four contiguous substrings of b of length |a|: "00", "01", "11", and "11". The distance between "01" and "00" is |0 - 0| + |1 - 0| = 1. The distance between "01" and "01" is |0 - 0| + |1 - 1| = 0. The distance between "01" and "11" is|0 - 1| + |1 - 1| = 1. Last distance counts twice, as there are two occurrences of string "11". The sum of these edit distances is1 + 0 + 1 + 1 = 3.

The second sample case is described in the statement.


#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1,class T2>inline void gmax(T1 &a,T2 b){if(b>a)a=b;}
template <class T1,class T2>inline void gmin(T1 &a,T2 b){if(b<a)a=b;}
const int N=2e5+10,M=0,Z=1e9+7,ms63=0x3f3f3f3f;
int casenum,casei;
char a[N],b[N];
int f[2][N];
int main()
{
	while(~scanf("%s%s",a+1,b+1))
	{
		int n=strlen(a+1);
		int m=strlen(b+1);
		for(int i=1;i<=m;++i)
		{
			f[0][i]=f[0][i-1]+(b[i]=='0');
			f[1][i]=f[1][i-1]+(b[i]=='1');
		}
		LL ans=0;
		for(int i=1;i<=n;++i)
		{
			int l=i;
			int r=m-(n-i);
			if(a[i]=='0')ans+=f[1][r]-f[1][l-1];
			else ans+=f[0][r]-f[0][l-1];
		}
		printf("%lld\n",ans);
	}
	return 0;
}
/*
【题意】
给你一个模板串a和匹配串b,|a|<=|b|<=2e5,两个串都是01串。
于是,a串之于b串的匹配位置,就有|b|-|a|+1种
我们想问,对于所有的匹配位置,会产生的匹配差异总和是多少。

所以匹配差异,是指——
比如模板串是0011,匹配0101,对于所有'0' match '1' 或者'1' match '0 ',
都产生了1的匹配差异。即这个样例产生了2的匹配差异。

【类型】
水题 前缀和

【分析】
我们直接枚举a串的每个位置,它会对应出b串的一个匹配区间。
我们只要查看区间中有多少字符与其不同即可。
这个可以利用前缀和简单实现。

【时间复杂度&&优化】
O(n+m)

*/

版权声明:题解中哪里写错请一定要指出来QwQ 转载还请注明下出处哦,谢谢^_^

相关文章推荐

Codeforces Round #336 (Div. 2) B. Hamming Distance Sum

B. Hamming Distance Sum time limit per test 2 seconds memory limit per test 256 megabytes inpu...

【Codeforces Round 336 (Div 2) C】【贪心 DP思维】Chain Reaction 每个灯塔位置为a[]破坏b[]范围所有灯塔 设置一个灯塔使得最多灯塔被保留

C. Chain Reaction time limit per test 2 seconds memory limit per test 256 megabytes ...

【Codeforces Round 336 (Div 2) A】【贪心 水题】Saitama Destroys Hotel 电梯只下不上 接n人到达0层最早时刻

A. Saitama Destroys Hotel time limit per test 1 second memory limit per test 256 megabyt...

Codeforces Round #207 (Div. 2) Xenia and Hamming(字符串匹配)

http://codeforces.com/contest/357/problem/D D. Xenia and Hamming time limit per test 1 second ...

【Codeforces Round 331 (Div 2)B】【水题 贪心】Wilbur and Array 修改对p位置后的所有位置生效 最少修改次数使得数列变成b

Wilbur and Array time limit per test 2 seconds memory limit per test 256 megabytes ...

【Codeforces Round 339 (Div 2)B】【水题】Gena's Code 若干10数+1特殊数的乘积

B. Gena's Code time limit per test 0.5 seconds memory limit per test 256 megabytes ...

【Codeforces Round 273 (Div 2)A】【水题 语文题】 Initial Bet 5人初始硬币都为b,告诉你最后的硬币数让你输出合法正整数b

A. Initial Bet time limit per test 1 second memory limit per test 256 megabytes inp...

【Codeforces Round 354 (Div 2)C】【前缀和二分or双指针】Vasya and String ab序列最多改变k位置的最长同字符子串长度

C. Vasya and String time limit per test 1 second memory limit per test 256 megabytes ...

Codeforces Round 370 (Div 2) D.Memory and Scores 【前缀和 DP】双人随机数值增减K A比B多的方案数

传送门  :codeforces 712D 描述: D. Memory and Scores time limit per test 2 seconds ...

Codeforces Round #365 (Div. 2) 703B Mishka and trip 水题

Mishka and trip time limit per test 1 second memory limit per test 256 megabytes in...
  • kyoma
  • kyoma
  • 2016年08月05日 12:59
  • 265
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:【Codeforces Round 336 (Div 2) B】【水题 前缀和】Hamming Distance Sum 双字符串所有位置匹配下的差异数之和
举报原因:
原因补充:

(最多只允许输入30个字)