公式

B - 山寺の札 / Tags at the Mountain Temple 解説 by admin

gpt-6-astra-medium

Overview

Swap the characters at the specified \(2\) positions in the string in the order they are given in the input. Output the string after all swaps have been performed.

Analysis

This problem can be solved by directly simulating the operations described in the problem statement. No special optimizations are required.

For example, when the string is abcd, swapping the \(1\)-st and \(3\)-rd characters yields cbad. Next, swapping the \(2\)-nd and \(4\)-th characters yields cdab. In this way, each operation is performed on the state resulting from all previous swaps.

In Python, strings are immutable, so individual characters cannot be modified directly. Therefore, we convert the string into a list of characters. With a list, we can swap two specified elements in \(O(1)\) time.

Note that reconstructing the string after every swap would also easily pass within the time limit under the constraints of this problem. However, using a list makes the implementation simpler and more efficient.

Be careful that sequential assignment like the following will not correctly swap the characters:

s[a - 1] = s[b - 1]
s[b - 1] = s[a - 1]

Because the original s[a - 1] is overwritten in the first assignment, both elements end up having the same character. We can avoid this issue by using simultaneous assignment in Python.

Algorithm

  1. Read \(N, Q\), and the string \(S\), then convert \(S\) into a list of characters s.
  2. Perform the following process \(Q\) times:
    • Read the positions \(a, b\) to be swapped.
    • Swap s[a - 1] and s[b - 1].
  3. Concatenate the characters in the list using "".join(s) and output the result.

In each operation, only the specified \(2\) positions are swapped, and the rest remain unchanged. Therefore, by performing the operations in the given order, the list will always represent the current state, and the desired string will be obtained at the end.

Complexity

  • Time Complexity: \(O(N + Q)\)
    • Converting to a list and creating the final string each take \(O(N)\), and the \(Q\) swaps take \(O(Q)\) in total.
  • Space Complexity: \(O(N)\)
    • Required for storing the list of \(N\) characters. The swap queries can be processed on the fly, so they do not all need to be stored in memory.

Implementation Notes

  • The positions in the problem statement are \(1\)-indexed, whereas Python list indices are \(0\)-indexed. Always subtract \(1\) when accessing elements.
  • Use simultaneous assignment for swapping. Since the expressions on the right-hand side are evaluated first, the elements can be swapped without losing the original characters.
  s[a - 1], s[b - 1] = s[b - 1], s[a - 1]
  • Even if \(a = b\), this operation simply swaps an element with itself, so no conditional branch is required.

  • When printing the output, do not print the list directly; convert it back to a string using "".join(s).

    Source Code

# 問題文から独立に実装: リストにして 1-indexed の位置を入れ替える
n, q = map(int, input().split())
s = list(input().strip())
for _ in range(q):
    a, b = map(int, input().split())
    s[a - 1], s[b - 1] = s[b - 1], s[a - 1]
print("".join(s))

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