B - 部分文字列の書き換え / Substring Rewriting Editorial by admin
Gemini 3.8 Flash (High)Overview
This problem asks for the minimum number of characters that need to be changed in a string \(S\) so that some contiguous substring of length \(M\) matches the string \(T\).
Analysis
For string \(S\) to contain \(T\) as a contiguous substring, some contiguous segment of length \(M\) in \(S\) must be made to match \(T\) completely.
Here, let us consider fixing the starting position of the segment that will match \(T\). For example, if we decide to match the substring of length \(M\) starting at the \(i\)-th character of \(S\) with \(T\), how many character replacements are required? Positions where the characters of \(S\) and \(T\) already match do not need to be changed; we only need to replace characters at positions where they differ with the corresponding characters of \(T\). Therefore, the number of required replacements is simply the number of positions where the corresponding characters differ.
Example
When \(S = \text{"codeforces"}\ (N = 10)\) and \(T = \text{"atcoder"}\ (M = 7)\):
- Comparing starting at index 0 ("codefor" and "atcoder"):
- c \(\neq\) a
- o \(\neq\) t
- d \(\neq\) c
- e \(\neq\) o
- f \(\neq\) d
- o \(\neq\) e
- r \(=\) r
- Number of differing characters: \(6\)
- Comparing starting at index 1 ("odeforc" and "atcoder"): \(7\) differing characters
- Comparing starting at index 2 ("deforce" and "atcoder"): \(7\) differing characters
- Comparing starting at index 3 ("eforces" and "atcoder"): \(7\) differing characters
In this example, the minimum number of character replacements is \(6\).
Feasibility of Brute-Force Search
There are only \(N - M + 1\) possible starting positions for a substring of length \(M\) in \(S\), from index \(0\) to index \(N - M\).
Looking at the constraints, \(N, M \leq 5000\). If we compare each character of \(T\) (\(M\) characters) for all possible starting positions, the total number of character comparisons is at most \((N - M + 1) \times M\). This value is maximized when \(N = 5000\) and \(M = 2500\), which is approximately \(2501 \times 2500 \approx 6.25 \times 10^6\) operations.
In competitive programming, a typical rule of thumb is that around \(10^7 \sim 10^8\) loop iterations can be executed per second, so this brute-force approach will comfortably pass within the time limit.
Algorithm
- Initialize a variable
min_diffto store the minimum answer, setting it to the maximum possible value of \(M\) (since replacing all characters requires at most \(M\) changes). - Iterate the starting position \(i\) from \(0\) to \(N - M\) (outer loop).
- For each \(i\), initialize a variable
diffto \(0\) to count the number of differing characters. - Iterate \(j\) from \(0\) to \(M - 1\), and increment
diffby \(1\) whenever \(S[i + j] \neq T[j]\) (inner loop). - After the inner loop finishes, update the minimum value with
min_diff = min(min_diff, diff). - After testing all starting positions, output
min_diff.
Complexity
- Time Complexity: \(O((N - M + 1) \times M)\)
- The outer loop runs \(N - M + 1\) times, and the inner loop runs \(M\) times. Even in the worst case, this requires about \(6.25 \times 10^6\) operations, which easily runs well within the time limit.
- Space Complexity: \(O(N + M)\)
- Only memory to store the input strings \(S\) and \(T\) is consumed.
Implementation Notes
0-indexed Index Range: With 0-based indexing (counting characters starting from 0), the valid range for the starting position \(i\) of the substring in \(S\) is \(0 \leq i \leq N - M\). Write the loop condition as
i <= n - mto prevent out-of-bounds access.Setting the Initial Value: Since replacing all characters takes at most \(M\) operations, initializing
min_diffto \(M\) (or any sufficiently large value) ensures that the minimum can be updated correctly.Source Code
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
if (!(cin >> n >> m)) return 0;
string s, t;
cin >> s >> t;
int min_diff = m;
for (int i = 0; i <= n - m; ++i) {
int diff = 0;
for (int j = 0; j < m; ++j) {
if (s[i + j] != t[j]) {
diff++;
}
}
min_diff = min(min_diff, diff);
}
cout << min_diff << "\n";
return 0;
}
This editorial was generated by gemini-3.8-flash-high.