B - 部分文字列の書き換え / Substring Rewriting Editorial by admin
Claude Opus 5 (High)Overview
In this problem, we are asked to find the minimum number of characters that need to be rewritten to “embed” a string \(T\) of length \(M\) into a string \(S\) of length \(N\). We can test all possible positions where \(T\) can be placed and find the minimum number of mismatched characters (the Hamming distance).
Analysis
Key Observation 1: We only need to rewrite characters within the range where \(T\) is placed
Ultimately, we only need the substring of \(S\) from the \(i\)-th character to the \((i+M-1)\)-th character to match \(T\). There is no reason to rewrite any characters outside this range (rewriting them does not help satisfy the condition and only increases the cost).
Therefore, once the starting position \(i\) where \(T\) is placed is fixed, the required number of rewrites is uniquely determined:
- Any position \(j\) where \(S_{i+j-1} \ne T_j\) must be rewritten (cost 1 per character).
- Any position where \(S_{i+j-1} = T_j\) does not need to be rewritten.
In other words, the cost for a starting position \(i\) is $\(\mathrm{cost}(i) = \#\{\, j \mid 1 \le j \le M,\ S_{i+j-1} \ne T_j \,\}\)\( which is simply the **Hamming distance** between the substring of \)S\( and \)T$.
Key Observation 2: We can brute-force all starting positions
There are at most \(N\) possible starting positions where \(T\) can be placed, specifically \(i = 1, 2, \dots, N-M+1\). There is no need to cleverly figure out “which position is optimal”; we can simply test every position and take the minimum.
Example
When \(S = \) abcbcdbc, \(T = \) bcc (\(N=8, M=3\)):
| Starting position \(i\) | Substring of \(S\) | Number of mismatches with \(T\) |
|---|---|---|
| 1 | abc |
3 (a≠b, b≠c, c≠c is a match → actually 2) |
| 2 | bcb |
1 (3rd character b≠c) |
| 3 | cbc |
3 |
| 4 | bcd |
1 |
| 5 | cdb |
3 |
| 6 | dbc |
2 |
The minimum is \(1\), so the answer is \(1\).
Is a naive approach fast enough?
Trying all patterns of “which characters of \(S\) to rewrite to what” would result in \(26^N\) possibilities, which is completely infeasible. However, based on the observation above, the only thing we need to search is the “starting position to place \(T\)”, and checking each position takes \(O(M)\) time. This gives an overall time complexity of \(O(NM)\). Since \(N, M \le 5000\), there are at most about \(5000 \times 5000 = 2.5 \times 10^7\) comparisons, which comfortably passes within the time limit in C++.
(If you need an even faster approach, techniques such as convolution using bitsets for each character \(c\) or FFT-based string matching exist, but they are unnecessary under these constraints.)
Algorithm
- Initialize \(best \leftarrow \infty\).
- For each starting position \(i = 0, 1, \dots, N-M\) (0-indexed), perform the following:
- \(cnt \leftarrow 0\)
- For each \(j = 0, 1, \dots, M-1\), if \(S_{i+j} \ne T_j\), increment \(cnt\) by 1.
- \(best \leftarrow \min(best, cnt)\)
- Output \(best\).
Since \(M \le N\) is guaranteed, the loop will always run at least once, and \(best\) will never remain \(\infty\).
Complexity
- Time complexity: \(O(NM)\) (at most about \(2.5 \times 10^7\) character comparisons)
- Space complexity: \(O(N + M)\) (only for storing the input strings)
Implementation Notes
Using 0-indexing, the loop condition is
i + M <= N(or \(i \le N-M+1\) in 1-indexing). Be careful of off-by-one errors.Extracting the substring each time using
substrincurs a large constant factor overhead due to string copying. Directly compare characters using index access likeS[i+j] != T[j].Initialize the minimum value with a sufficiently large value such as
INT_MAX(since the cost is at most \(M \le 5000\),M+1is also sufficient).In slower languages like Python, \(O(NM)\) can be tight. In that case, utilizing built-in functions like
sum(a != b for a, b in zip(...))or speeding it up by maintaining character occurrences as bitmasks (using Python’s arbitrary-precision integers) can be effective.Source Code
#include <bits/stdc++.h>
using namespace std;
int main(){
int N, M;
string S, T;
if(!(cin >> N >> M >> S >> T)) return 0;
int best = INT_MAX;
for(int i = 0; i + M <= N; i++){
int cnt = 0;
for(int j = 0; j < M; j++) if(S[i+j] != T[j]) cnt++;
best = min(best, cnt);
}
cout << best << "\n";
return 0;
}
This editorial was generated by claude5opus-high.