【Codeforces Round 336 (Div 2) B】【水题 前缀和】Hamming Distance Sum 双字符串所有位置匹配下的差异数之和
2015-12-25 11:46
681 查看
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
output
input
output
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.
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 ,b ; int f[2] ; 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) */
相关文章推荐
- HDU 5240 Exam (好水的题)
- 北大—1006——Biorhythms
- 时间计算(heaven.pas/cpp)
- 工作依赖(job.cpp/pas)
- 题目 英雄 (BFS)
- kmp 学习 hihocoder #1015
- HDU 1096 A+B for Input-Output Practice (VIII)
- P1478
- P1035
- P1008 难度2.7
- 2020 绝对值排序
- 2021 发工资咯
- 2022 海选女主角
- 2024 C语言合法标识符
- 2025 查找最大元素
- 2026 首字母变大写
- 2027 统计元音
- 2028 Lowest Common Multiple Plus
- 2018 母牛的故事
- 2023 求平均成绩