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
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.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to compute the sum of Hamming distances between string a (length n) and all contiguous substrings of b (length m) of length n. There are m-n+1 substrings. Naively O(n*(m-n+1)) is too slow because n and m up to 200k, product up to 4e10.
We need to compute efficiently.
Define a = a[0..n-1], b = b[0..m-1]. For each shift i from 0 to m-n, we compute distance d_i = sum_{j=0}^{n-1} |a[j] - b[i+j]|.
We need total sum S = sum_{i=0}^{m-n} d_i.
We can rewrite S = sum_{i=0}^{m-n} sum_{j=0}^{n-1} |a[j] - b[i+j]| = sum_{j=0}^{n-1} sum_{i=0}^{m-n} |a[j] - b[i+j]|.
Swap summations: For each position j in a, we consider its contribution: for each shift i, we compare a[j] with b[i+j]. As i varies, the index in b that aligns with a[j] is k = i + j, where i from 0 to m-n, so k ranges from j to j + (m-n) = j + (m-n). But note that b length is m, indices from 0 to m-1. Check: i max = m-n, then k = (m-n)+j. Since j <= n-1, max k <= (m-n)+(n-1) = m-1, ok. So for fixed j, the set of b indices that a[j] compares to are k = j, j+1, ..., j+(m-n). That's a contiguous segment of b of length (m-n+1). So contribution of position j is sum_{k=j}^{j+(m-n)} |a[j] - b[k]|.
Let a_j be 0 or 1. Then |a_j - b_k| = 1 if they differ, else 0. So it's essentially count of positions in that segment of b where b_k != a_j.
Thus total S = sum_{j=0}^{n-1} (number of k in [j, j+(m-n)] such that b_k != a_j).
So we need for each j, count of mismatches in a sliding window of b. That's like: For each j, we have a window start = j, length L = m-n+1. And we want count of positions in b in that window that are opposite of a_j.
We can precompute prefix sums for b to quickly get number of ones and zeros in any interval.
Let prefix1[k] = number of '1's in b[0..k-1] (or up to index). Then in interval [l, r] inclusive, count1 = prefix1[r+1] - prefix1[l]; count0 = length - count1.
If a_j = 0, then mismatches = count1 in window. If a_j = 1, mismatches = count0 in window.
Thus S = sum_{j=0}^{n-1} (if a_j == 0 then count1(j) else count0(j)), where count1(j) = number of 1s in b from j to j+L-1 inclusive, L = m-n+1.
Compute prefix array for b: let m = len(b). pre[i] = number of 1's in b[0..i-1] (size m+1). pre[0]=0; for i in 0..m-1: pre[i+1] = pre[i] + (b[i]=='1').
Then for each j in 0..n-1, left = j, right = j+L-1. count1 = pre[right+1] - pre[left].
if a_j == '0': add count1, else add (L - count1).
Complexities O(n+m). That works because n,m up to 200k.
Let's test with sample.
First sample: a="01", n=2; b="00111", m=5; L = m-n+1 = 4. For j=0, a0='0', left=0, right=3? Actually j+L-1 = 0+4-1=3; b[0..3]="0011": ones count = 2? Actually "0,0,1,1" => 2 ones. So add 2. For j=1, a1='1', left=1, right=4? 1+3=4; b[1..4]="0111": ones count = 3? Actually "0,1,1,1" => 3 ones. L=4, count0 = 4-3=1. So add 1. Total = 3. matches output.
Second sample: a="0011", n=4; b="0110", m=4; L = 4-4+1 = 1. So each j compares to single character. For j=0 a0='0', b[0]='0': mismatch 0; j=1 a1='0', b[1]='1': mismatch 1; j=2 a2='1', b[2]='1': 0; j=3 a3='1', b[3]='0': 1; total 2. matches.
Edge cases: n = 1, then L = m. For each j only one j. For a[0]=0, add count1 in whole b; else count0. That makes sense: total sum of distances between single char a and each substring of b length 1 (i.e., each character), so sum over all positions of b: if a[0]=0 then distance =1 if b[i]=1 else 0, so total = number of ones. That matches.
If n = m, L = 1, each j maps to a single b character. So total is Hamming distance between a and b, as only one substring.
So solution is straightforward.
But wait, is there any trick? Input sizes up to 200k, but the sum might be large: worst-case all mismatches: n*(m-n+1). n,m up to 200k, so max product about 4e10, which fits in 64-bit (since 4e10 < 2^63). Use long long (int64_t).
Thus algorithm: read strings a and b. n = a.size(), m = b.size(). L = m - n + 1. Compute prefix array of b ones: vector pre(m+1,0). For i in 0..m-1: pre[i+1] = pre[i] + (b[i] - '0'). Then iterate j from 0 to n-1: left = j, right = j + L - 1. count1 = pre[right+1] - pre[left]. if a[j]=='0': total += count1; else total += (L - count1). Print total.
Make sure right index doesn't exceed m-1: Since L = m-n+1, right = j+L-1 = j+m-n = j + (m-n). For j max = n-1, right = n-1 + m - n = m-1, ok. So safe.
Complexities O(m+n). That's good.
Now produce final answer in required format: just integer.
We'll implement in C++.
But the problem statement originally from Codeforces? I recall a problem "Hamming Distance Sum" (CF 609B? Actually 608B? I think it's "Hamming Distance Sum" from Codeforces Round #...). The solution described is exactly this.
We need to be careful: input constraints: 1 ≤ |a| ≤ 200k, |a| ≤ |b| ≤ 200k. So L is at least 1.
We must use fast I/O.
Alright.
Thus final answer: Provide solution explanation and code.
But user asked: "Genos needs your help. He was asked to solve the following programming problem by Saitama: ..." They likely expect solution and code. So we provide a response with explanation and C++ code.
But note: The problem includes an image, but we ignore.
We'll produce the final answer.
Let's produce final response. We can compute the sum efficiently by swapping summations and using prefix sums on string b.
For each shift i (0 ≤ i ≤ |b|‑|a|), the Hamming distance between a and the substring b[i .. i+|a|‑1] is
For a fixed j, the inner sum runs over the indices k = i+j where i ranges from 0 to |b|-|a|. Hence k goes from j to j+|b|-|a| (a segment of length L = |b|-|a|+1). The contribution of position j is the number of characters in b[j .. j+L‑1] that differ from a_j:
if a_j = '0', contribution = number of '1's in that segment,
if a_j = '1', contribution = number of '0's in that segment (i.e. L − number of '1's).
We can quickly obtain the number of '1's in any interval of b using a prefix sum array.
Algorithm
Let n = |a|, m = |b|, L = m − n + 1.
Build array pre[0..m] where pre[i] = total number of '1's in b[0 .. i‑1].
pre[0] = 0
For i = 0 .. m‑1: pre[i+1] = pre[i] + (b[i] == '1').
Initialize total = 0 (64‑bit integer).
For each j = 0 .. n‑1:
left = j
right = j + L - 1
ones = pre[right+1] - pre[left]
If a[j] == '0': total += ones
Else: total += (L - ones)
Print total.
Time complexity: O(n + m)
Space complexity: O(m)
cpp
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
string a, b;
cin >> a >> b;
int n = (int)a.size(), m = (int)b.size();
int L = m - n + 1; // length of each window in b
// prefix sum of ones in b
vector<int> pre(m + 1, 0);
for (int i = 0; i < m; ++i) {
pre[i + 1] = pre[i] + (b[i] == '1');
}
ll ans = 0;
for (int j = 0; j < n; ++j) {
int left = j;
int right = j + L - 1; // inclusive
int ones = pre[right + 1] - pre[left];
if (a[j] == '0') {
ans += ones;
} else {
ans += (L - ones);
}
}
cout << ans << '\n';
return 0;
}