【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 转载还请注明下出处哦,谢谢^_^ https://blog.csdn.net/snowy_smile/article/details/50402262

【Codeforces 608B】Hamming Distance Sum(前缀和)

Hamming Distance SumDescription Genos needs your help. He was asked to solve the following programm...
  • Chen_yuazzy
  • Chen_yuazzy
  • 2017-08-10 22:29:39
  • 119

cf#336-B-Hamming Distance Sum

http://codeforces.com/contest/608/problem/B 给a,b串,求a和  【b的所有长度为len(a)的连续子串的】的  Hamming Distance...
  • viphong
  • viphong
  • 2016-01-07 20:18:22
  • 787

汉明距离(Hamming distance)

两个字符串之间的汉明距离是指两个相等长度的字符串,对应位置上不同字符的个数。例子如下:A=abcdefB=adddef则A与B之间的汉明距离是2,因为第二位和第三位不同。...
  • lgfun
  • lgfun
  • 2016-09-09 11:51:21
  • 5019

codeforces 608B Hamming Distance Sum

B. Hamming Distance Sum time limit per test 2 seconds memory limit per test 256 megabytes input...
  • liyunlong41
  • liyunlong41
  • 2015-12-26 22:49:15
  • 583

【解题报告】 Educational Codeforces Round 19

简略的解题报告。
  • TRiddle
  • TRiddle
  • 2017-04-16 17:16:52
  • 943

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...
  • rikkatheworld
  • rikkatheworld
  • 2015-12-26 17:41:14
  • 371

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

B. Hamming Distance Sum time limit per test 2 seconds memory limit per test 256 megabyte...
  • helloiamclh
  • helloiamclh
  • 2015-12-29 17:29:21
  • 350

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

B. Hamming Distance Sum time limit per test 2 seconds memory limit per test 256 megabyte...
  • ZZ_AC
  • ZZ_AC
  • 2015-12-24 18:22:45
  • 455

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

B. Hamming Distance Sum time limit per test 2 seconds memory limit per test 256 megabyte...
  • GKHack
  • GKHack
  • 2015-12-29 00:40:32
  • 676

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 input...
  • KeyboardMagician
  • KeyboardMagician
  • 2015-12-25 07:54:55
  • 524
收藏助手
不良信息举报
您举报文章:【Codeforces Round 336 (Div 2) B】【水题 前缀和】Hamming Distance Sum 双字符串所有位置匹配下的差异数之和
举报原因:
原因补充:

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