Official

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

Gemini 3.8 Flash (High)

Overview

This problem asks for the minimum number of characters in a string \(S\) of length \(N\) that must be changed so that a string \(T\) of length \(M\) is contained as a contiguous substring.

Analysis

Consider fixing the starting position of \(T\) in \(S\).

When making the substring of length \(M\) starting at the \(i\)-th character of \(S\) match \(T\), the number of characters to change is equal to the number of differing characters between the \(i\)-th through \((i + M - 1)\)-th characters of \(S\) and the \(1\)-st through \(M\)-th characters of \(T\).

There are \(N - M + 1\) possible starting positions, given by \(1 \leq i \leq N - M + 1\). By counting the number of mismatched characters for each starting position and taking the minimum, we can find the answer.

Now, let us estimate the computational complexity: - Number of candidate starting positions: at most about \(N\) - Number of character comparisons at each position: \(M\) - Total number of comparisons: at most \((N - M + 1) \times M\)

The constraints are \(N, M \leq 5000\). The value \((N - M + 1) \times M\) is maximized when \(M \approx \frac{N}{2} = 2500\), where the number of operations is at most \(2500 \times 2500 \approx 6.25 \times 10^6\) (about \(6.25 \times 10^6\)). Since this is well within the number of operations a computer can process in one second (roughly \(10^7 \sim 10^8\)), a brute-force approach trying all starting positions will comfortably run within the time limit.

Algorithm

  1. Initialize a variable ans representing the candidate minimum answer to the maximum possible value, \(M\) (since replacing all characters will always make them match).
  2. Iterate the starting index \(i\) from \(0\) to \(N - M\) (0-indexed).
  3. For each \(i\), initialize the mismatch count diff to \(0\), and compare \(S[i + j]\) and \(T[j]\) for each character index \(j\) (\(0 \leq j < M\)).
    • If \(S[i + j] \neq T[j]\), increment diff by \(1\).
    • If diff becomes greater than or equal to the current minimum ans, terminate the inner loop early (pruning), as it can no longer update ans.
  4. After the inner loop terminates, if diff < ans, update ans with diff.
    • If ans becomes \(0\), terminate the outer loop as well, since it cannot become any smaller.
  5. Output the final value of ans.

Complexity

  • Time Complexity: \(O((N - M + 1) \times M)\)
    • Even in the worst case, the number of operations is about \(6.25 \times 10^6\), and with pruning, it runs even faster on average.
  • Space Complexity: \(O(N + M)\)
    • It only consumes memory to store the input strings \(S\) and \(T\).

Key Implementation Points

  • Optimization via Pruning: By aborting the comparison (breaking) as soon as the number of mismatched characters reaches or exceeds the current minimum ans, we can significantly reduce redundant comparisons. Furthermore, exiting the entire search when ans == 0 allows the program to terminate immediately once an exact match is found.

  • Index Range: The starting position \(i\) ranges from \(0\) to \(N - M\) in 0-indexed terms (range(n - m + 1)). Be careful to avoid out-of-range index errors (Index Out of Range).

    Source Code

import sys


def main():
    input_data = sys.stdin.read().split()
    if not input_data:
        return

    n = int(input_data[0])
    m = int(input_data[1])
    s = input_data[2]
    t = input_data[3]

    ans = m

    for i in range(n - m + 1):
        diff = 0
        for j in range(m):
            if s[i + j] != t[j]:
                diff += 1
                if diff >= ans:
                    break
        if diff < ans:
            ans = diff
            if ans == 0:
                break

    print(ans)


if __name__ == "__main__":
    main()

This editorial was generated by gemini-3.8-flash-high.

posted:
last update: