CF608B.Hamming Distance Sum

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 s__i is the i-th character of s and t__i 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|.

Genos 需要你的帮助。Saitama 给他布置了如下编程题目:

字符串 $ s $ 的长度记为 ∣s∣|s|。对于两个等长的字符串 $ s $ 和 $ t $,其汉明距离定义为
,
其中 $ s_i $ 表示 $ s $ 的第 $ i $ 个字符,$ t_i $ 表示 $ t $ 的第 $ i $ 个字符。例如,字符串 "0011" 与字符串 "0110" 的汉明距离为
$ |0 - 0| + |0 - 1| + |1 - 1| + |1 - 0| = 0 + 1 + 0 + 1 = 2 $。

给定两个二进制字符串 $ a $ 和 $ b $,求 $ a $ 与 $ b $ 的所有长度为 ∣a∣|a| 的连续子串之间的汉明距离之和。

输入格式

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.

输入的第一行包含二进制字符串 aa(1 ≤ ∣a∣ ≤ 200 0001 \le |a| \le 200\,000)。

输入的第二行包含二进制字符串 bb(∣a∣ ≤ ∣b∣ ≤ 200 000|a| \le |b| \le 200\,000)。

保证两个字符串仅由字符 '0' 和 '1' 组成。

输出格式

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

输出一个整数——即字符串 aa 与字符串 bb 的所有长度为 ∣a∣|a| 的连续子串之间的汉明距离之和。

输入输出样例

  • 输入#1

    01
    00111

    输出#1

    3
  • 输入#2

    0011
    0110

    输出#2

    2

说明/提示

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.

对于第一个样例,字符串 b 中长度为 |a| 的连续子串共有四个:“00”、“01”、“11” 和 “11”。字符串 “01” 与 “00” 的距离为 |0 − 0| + |1 − 0| = 1;“01” 与 “01” 的距离为 |0 − 0| + |1 − 1| = 0;“01” 与 “11” 的距离为 |0 − 1| + |1 − 1| = 1。由于字符串 “11” 出现了两次,最后一项距离需计算两次。这些编辑距离之和为 1 + 0 + 1 + 1 = 3。

第二个样例已在题目描述中给出。

输入解题思路,AI测评打分。不知道怎么写?

首页