Official

B - 部分文字列の書き換え / Substring Rewriting Editorial by admin

gpt-6-astra-medium

Overview

We examine all contiguous substrings of \(S\) of length \(M\) and count the number of characters that differ from \(T\). The minimum of these counts is the required number of rewrites.

Analysis

Fixing the Position of \(T\)

First, consider deciding where \(T\) will be placed in \(S\) after the rewrites. In the following, strings are \(0\)-indexed.

If we place \(T\) starting at index \(i\) of \(S\), for each \(j\) (\(0 \leq j < M\)), we should do the following:

  • If \(S[i+j] = T[j]\), no rewrite is needed.
  • If \(S[i+j] \neq T[j]\), we must rewrite \(S[i+j]\) to \(T[j]\).

In other words, the minimum number of rewrites for a fixed starting position is the number of positions where the corresponding characters differ. There is no need to rewrite characters outside the target range.

For example, when \(S = \texttt{abcde}\) and \(T = \texttt{bxd}\), the comparison results with each substring of length \(3\) are as follows:

Starting position \(i\) Substring Number of characters differing from \(T\)
\(0\) abc \(3\)
\(1\) bcd \(1\)
\(2\) cde \(3\)

Rewriting c in bcd to x turns \(S\) into abxde, which contains bxd. Therefore, the answer is \(1\).

Searching Over Starting Positions Instead of Rewriting Methods

Trying every combination of positions to rewrite and characters to replace them with would result in an enormous number of candidates, making it impossible to finish within the time limit.

However, once we fix the starting position of \(T\), the required rewrites are uniquely determined. There are only \(N-M+1\) possible starting positions, from \(0\) to \(N-M\).

Since we only need to compare \(M\) characters for each candidate, this simple brute-force search is well within the time limit under the given constraints.

Furthermore, no matter how the goal is achieved, \(T\) must appear somewhere in \(S\) after the rewrites. All characters that originally differed at that starting position must be rewritten, and conversely, rewriting only those characters is sufficient to achieve the goal. Therefore, the minimum number of mismatches across all starting positions is the answer.

Algorithm

  1. Initialize a variable answer to store the result with \(M\).
    • This is because rewriting all characters in a range of length \(M\) can always produce \(T\).
  2. Check each starting position \(i\) from \(0\) to \(N-M\) in order:
    1. Set the mismatch count changes to \(0\).
    2. For \(j = 0,1,\ldots,M-1\), if \(S[i+j]\) and \(T[j]\) differ, increment changes by \(1\).
    3. Update answer to the minimum of the current answer and changes.
    4. If answer is \(0\), terminate the search early since it cannot get any smaller.
  3. Output answer.

Complexity

  • Time complexity: \(O((N-M+1)M)\), or roughly \(O(NM)\).
  • Space complexity: \(O(N+M)\) to store the input strings, and \(O(1)\) additional space.

Implementation Points

  • The range of the starting position \(i\) is \(0 \leq i \leq N-M\). To include the last starting position, set the outer loop condition to i <= N - M.

  • In the inner loop, compare S[i + j] with T[j].

  • Reset changes to \(0\) for each starting position.

  • There is no need to actually rewrite characters or create substrings; counting only the mismatches is sufficient.

  • Early termination when the answer reaches \(0\) is an optimization; omitting it will still yield the correct answer.

    Source Code

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, M;
    string S, T;
    cin >> N >> M >> S >> T;

    int answer = M;
    for (int i = 0; i <= N - M; ++i) {
        int changes = 0;
        for (int j = 0; j < M; ++j) {
            if (S[i + j] != T[j]) {
                ++changes;
            }
        }
        answer = min(answer, changes);
        if (answer == 0) break;
    }

    cout << answer << '\n';
    return 0;
}

This editorial was generated by gpt-6-astra-medium.