Official

C - 文字の一括変換 / Bulk Character Conversion Editorial by admin

Qwen3-Coder-480B

Overview

This problem requires applying a series of specified character replacement operations in order to multiple strings and outputting the results.

Analysis

In this problem, we need to perform up to \(10^5\) replacement operations on multiple strings whose total length can be up to \(10^6\). If we actually modified all strings for every operation, it would take far too long (TLE).

The key insight is that we only need to track “what each lowercase letter ultimately transforms into.” In other words, instead of rewriting the strings for each operation, the crucial point is to efficiently manage the mapping of “original character → final character” across all operations.

An even more important idea is to build this mapping from back to front. For example, if there is an operation “a → b” and a later operation “b → c” has already been processed, then “a” ultimately becomes “c.” Since later operations have greater influence, processing from back to front allows us to determine the correct final state in a single pass.

Consider the following operations as an example: - Operation 1: ‘a’ → ‘b’ - Operation 2: ‘b’ → ‘c’

If processing from front to back, first ‘a’ becomes ‘b’, then we need to update so that ‘b’ (including the former ‘a’) becomes ‘c’. However, if processing from back to front, we first record ‘b’ → ‘c’, and then when processing ‘a’ → ‘b’, we can immediately determine that “a ultimately becomes c.”

In this way, by processing all operations from back to front to determine the final destination of each character, and then performing a batch conversion of each string according to this mapping, we can efficiently compute the answer.

Algorithm

  1. Prepare an array mapping that tracks the final transformation target for each lowercase letter (initially, each character maps to itself).
  2. Process the replacement operations from back to front, updating as mapping[a] = mapping[b].
  3. After processing all operations, perform a batch conversion on each string using mapping.
  4. Output the converted strings.

Complexity

  • Time complexity: \(O(\sum |S_i| + Q)\)
    (Each string is scanned only once, and each operation is processed in constant time)
  • Space complexity: \(O(26 + \sum |S_i|)\)
    (For the mapping table and the resulting strings)

Implementation Notes

  • Be mindful of processing operations from back to front.

  • Conversion between lowercase letters and indices (0–25) uses ord(c) - ord('a').

  • sys.stdin.read is used for fast input reading.

    Source Code

import sys

def main():
    input = sys.stdin.read
    data = input().split()
    
    N = int(data[0])
    Q = int(data[1])
    
    strings = data[2:2+N]
    operations = data[2+N:]
    
    # 文字の変換マッピングを後ろから構築する
    # 初期状態では各文字は自分自身にマッピングされる
    mapping = list(range(26))  # ord('a') + i の文字は最終的に ord('a') + mapping[i] になる
    
    # 操作を後ろから処理する
    for i in range(Q-1, -1, -1):
        a = ord(operations[2*i]) - ord('a')
        b = ord(operations[2*i+1]) - ord('a')
        # a から b への変換を追加する
        # すでに a が別の文字にマッピングされている場合、それを維持する
        # しかし、後ろから処理しているので、b 自体が最終的な値にマッピングされている
        # よって、a が最終的にどのような値になるかを b の最終値に設定する
        mapping[a] = mapping[b]
    
    # 各文字列を変換する
    result = []
    for s in strings:
        converted = []
        for c in s:
            converted.append(chr(ord('a') + mapping[ord(c) - ord('a')]))
        result.append(''.join(converted))
    
    print('\n'.join(result))

if __name__ == "__main__":
    main()

This editorial was generated by qwen3-coder-480b.

posted:
last update: