公式

A - ずらした暗号 / Shifted Cipher 解説 by admin 印刷ページ

gpt-6-astra-medium

Overview

Shift each character in the string \(S\) by \(K\) positions forward in alphabetical order. Since Z wraps around to A, the conversion can be performed using the remainder when divided by \(26\) (modulo \(26\)).

Analysis

There are \(26\) uppercase English letters in total. Therefore, we can map each character to an integer from \(0\) to \(25\) as follows:

  • A → \(0\)
  • B → \(1\)
  • \(\cdots\)
  • Z → \(25\)

Let \(x\) be the integer corresponding to a character. The position shifted \(K\) steps forward is simply \(x+K\). However, if this value is \(26\) or greater, it exceeds the range of uppercase English letters. Simply adding \(K\) directly to the character code will fail to wrap around from Z to A, resulting in a wrong answer.

This wrap-around can be represented using the remainder divided by \(26\). The position after the shift is:

\((x+K)\bmod 26\)

For example, Y corresponds to \(24\). When \(K=3\),

\((24+3)\bmod 26=1\)

which converts to B (corresponding to \(1\)).

Note that shifting each character \(1\) position at a time \(K\) times would also easily run within the time limit under the given constraints. However, using modulo allows us to convert each character in a single operation, keeping the implementation simple. In addition, shifting \(26\) times returns to the original character, so we can replace \(K\) with \(K\bmod 26\) at the beginning.

Algorithm

  1. Read \(K\) and \(S\), and replace \(K\) with \(K\bmod 26\).
  2. For each character c in \(S\), perform the following:
    • Convert the character to an integer \(x\) from \(0\) to \(25\) using ord(c) - ord('A').
    • Compute the shifted position using \((x+K)\bmod 26\).
    • Add ord('A') to this value and convert it back to an uppercase English letter using chr.
  3. Concatenate the converted characters in their original order and output the result.

Because the position shifted by \(K\) is computed modulo \(26\) for each character, the wrap-around from Z to A is properly handled as described in the problem statement.

Complexity

Let \(N=|S|\).

  • Time complexity: \(O(N)\)
    Each character is converted once.
  • Space complexity: \(O(N)\)
    To store the converted string.

Implementation Notes

  • ord converts a character to its character code, and chr converts a character code back to a character. The character codes for uppercase English letters are contiguous in alphabetical order.

  • The modulo operation is taken after adding \(K\) to the \(0\)-indexed position relative to A. In code, this corresponds to (ord(c) - ord('A') + K) % 26.

  • Using ''.join(...) allows you to concatenate the converted characters without any separator.

  • When \(K=0\) or \(K\) is a multiple of \(26\), the same logic naturally outputs the original string unchanged.

    Source Code

import sys

def main():
    data = sys.stdin.read().split()
    K = int(data[0]) % 26
    S = data[1]
    out = ''.join(chr(ord('A') + (ord(c) - ord('A') + K) % 26) for c in S)
    print(out)

main()

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

投稿日時:
最終更新: