codeforce 608B-Hamming Distance Sum (prefix sum)

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

Examples
Input
Copy
01
00111
Output
Copy
3
Input
Copy
0011
0110
Output
Copy
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.

题目大意:给你两个字符串,第一个字符串的长度小于等于第二个,然后拿第一个字符串1与字符串2从头到尾移动进行比较,求出此比较过程中出现的01对数。
解题思路:一开始我们肯定会想到暴力枚举,在字符串2上操作,拿字符串1从第一位往后移动进行比较,然后计算出每次移动所得的01对数,然而这样做复杂度太大,那么我们应该怎么来解决这个问题呢?这里给大家介绍另一种思路:对于字符串1中的每个字符,在比较时都是在一个确定的区间里移动的,所以我们只要求出这个区间中字符串2中与该字符不同的个数即可,对于不同的个数我们就可以用前缀和sum[a~b] = pre[b]-pre[a-1]来解决,这样我们的复杂度就降低到了O(b)。

AC代码:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<queue>
#define bug printf("***\n");
//#define mem0 memset(a, 0, sizeof(a));
using namespace std;
typedef pair<long long, int> par;
const int mod = 1e9+7;
const int INF = 1e9;
const int N = 10010;

int main()
{
    char str[N], ch[N];
    int pre0[N], pre1[N]; //前缀0的个数、前缀1的个数
    while(~scanf("%s%s", ch, str)) {
        long long ans = 0;
        int len1 = strlen(ch);
        int len2 = strlen(str);
        pre0[0] = pre1[0] = 0;
        if(str[0] == '0') pre0[0] = 1;
        else pre1[0] = 1;
        for(int i = 1; i < len2; i ++) { //统计前缀和
            if(str[i] == '1') {
                pre1[i] = pre1[i-1]+1;
                pre0[i] = pre0[i-1];
            }else {
                pre0[i] = pre0[i-1]+1;
                pre1[i] = pre1[i-1];
            }
        }
        for(int i = 0; i < len1;  i++) { //计算出匹配区间内所有不同的对数
            if(i == 0) {
                if(ch[i] == '0') ans += pre1[len2-len1];
                else ans += pre0[len2-len1];
            }else {
                if(ch[i] == '0') ans += (pre1[len2-len1+i] - pre1[i-1]);
                else ans + = (pre0 [len2-len1 + i] - pre0 [i-1]);
            }
        }
        printf("%lld\n", ans);
    }
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324750798&siteId=291194637