[python]백준 DRM Messages 15087번

김보현·2025년 1월 19일
0

[Bronze I] DRM Messages - 15087

문제 링크

문제 풀이

import sys
input = sys.stdin.readline
a = input().strip()
lth = len(a)
ha, hb = a[:lth//2], a[lth//2:]
dt = {'A':0, 'B':1, 'C':2, 'D':3, 'E':4, 'F':5, 'G':6, 'H':7, 'I':8, 'J':9, 'K':10, 'L':11, 'M':12, 'N':13, 'O':14, 'P':15, 'Q':16, 'R':17, 'S':18, 'T':19, 'U':20, 'V':21, 'W':22, 'X':23, 'Y':24, 'Z':25}
ra=0
rb=0
ra = sum(dt[ch] for ch in ha)
rb = sum(dt[ch] for ch in hb)

def rotate(string, rotation_value):
    rotated = ''
    for ch in string:
        rotated += chr((((ord(ch)-ord('A'))+rotation_value)%26)+ord('A'))
    return rotated
ha_rotated = rotate(ha, ra)
hb_rotated = rotate(hb, rb)
result = ''
for i in range(len(ha_rotated)):
    merged_ch = chr((((ord(ha_rotated[i])-ord('A'))+(ord(hb_rotated[i])-ord('A')))%26)+ord('A'))
    result += merged_ch
print(result)

(1) 입력 처리 및 분할
input().strip()으로 개행 문자 제거.
문자열을 반으로 정확히 나눔.
(2) 회전 값 계산
각 문자열의 문자 값의 합계를 계산하여 회전 값을 구함.
(3) 문자열 회전
각 문자열을 회전 값만큼 회전:
알파벳 문자 범위를 벗어나지 않도록 모듈러 연산(% 26)을 사용.
(4) 문자열 병합
두 문자열을 병합하며 최종 결과 생성:
각 문자에서 두 번째 문자열의 해당 문자 값만큼 추가 회전.
(5) 출력
최종 복호화된 결과를 출력.

성능 요약

메모리: 32412 KB, 시간: 40 ms

분류

구현, 문자열

제출 일자

2025년 1월 19일 12:10:50

문제 설명

DRM Encryption is a new kind of encryption. Given an encrypted string (which we’ll call a DRM message), the decryption process involves three steps: Divide, Rotate and Merge. This process is described in the following example with the DRM message “EWPGAJRB”:

  • Divide – First, divide the message in half to “EWPG” and “AJRB”.
  • Rotate – For each half, calculate its rotation value by summing up the values of each character (A = 0, B = 1, . . . Z = 25). The rotation value of “EWPG” is 4 + 22 + 15 + 6 = 47. Rotate each character in “EWPG” 47 positions forward (wrapping from Z to A when necessary) to obtain the new string “ZRKB”. Following the same process on “AJRB” results in “BKSC”.
  • Merge – The last step is to combine these new strings (“ZRKB” and “BKSC”) by rotating each character in the first string by the value of the corresponding character in the second string. For the first position, rotating ‘Z’ by ‘B’ means moving it forward 1 character, which wraps it around to ‘A’. Continuing this process for every character results in the final decrypted message, “ABCD”.

입력

The input contains a single DRM message to be decrypted. All characters in the string are uppercase letters and the string’s length is even and ≤ 15 000.

출력

Display the decrypted DRM message.

profile
Fall in love with Computer Vision

0개의 댓글

관련 채용 정보