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

来源:互联网 发布:网站发布到域名 编辑:程序博客网 时间:2024/06/05 10:46

Hamming Distance Sum

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


Example
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 is 1 + 0 + 1 + 1 = 3.

The second sample case is described in the statement.


题意:

有两个字符串a,b,|a|表示a串的长度,|b|表示b串的长度,|b|>=|a|,字符串只由01组成,求b的子串(长度也为|a|)与a对应相减的绝对值的和。求出所有子串并求和。

思路:

一开始写的暴力方法两重循环,虽然思路正确但是复杂度太高会超时。

for(int i=0;i+len1<=len2;i++)            {                for(int j=0;j<len1;j++)                {                    ans+=abs(a[j]-b[i+j]);                }            }

于是搜到了这样一种解法:前缀和法。此题01相减可以看作类似于异或运算。可以把每个位置之前的1和0数量统计并用数组标记。
此时对于数组b已有了一数组pre0和pre1,pre0[i]记录位置i之前0的个数,pre1[i]记录位置i之前1的个数。
之后遍历数组a,若字符为1,则计算数组b中此位置i到i+len2-len1位置的0的个数。(i-1是因为这个位置的0(若为0)也要算在区域之内).同理字符为0的时候也可得出结论。
如果已下面为例,形象的说
0101
10101010001010
即把上面的字符串依次向后移动直至末尾。a中每一个字符即为上述所说。

for(int i=1;i<=len1;i++)        {            if(a[i]=='1')            {                ans+=pre0[len2-len1+i]-pre0[i-1];            }            else if(a[i]=='0')            {                ans+=pre1[len2-len1+i]-pre1[i-1];             }         }

代码示例:

#include<iostream>#include<cstring>#include<cmath>using namespace std;#define MAX 200005char a[MAX],b[MAX];int pre0[MAX],pre1[MAX];//前缀和int main(){    while(cin>>(a+1)>>(b+1))    {        long long ans=0;         memset(pre0,0,sizeof(pre0));        memset(pre1,0,sizeof(pre1));        int len1,len2;        len1=strlen(a+1);        len2=strlen(b+1);         for(int i=1;i<=len2;i++)        {            if(b[i]=='1')            {                pre1[i]=pre1[i-1]+1;                pre0[i]=pre0[i-1];            }            else if(b[i]=='0')            {                pre0[i]=pre0[i-1]+1;                pre1[i]=pre1[i-1];            }         }               for(int i=1;i<=len1;i++)        {            if(a[i]=='1')            {                ans+=pre0[len2-len1+i]-pre0[i-1];            }            else if(a[i]=='0')            {                ans+=pre1[len2-len1+i]-pre1[i-1];             }         }        cout<<ans<<endl;     }     return 0; }