Leapfrog Encryption

면접 대비

시간 제한1초메모리 제한2048 MB

요약
키를 이용한 암호화 방식으로, 각 패스마다 방향을 바꾸며 빈 위치만 세어 평문이나 암호문을 배치하거나 복원한다.
난이도

보통10점 중 6점

유형
시뮬레이션, 구현, 배열, 문자열
정답자
아직 제출이 없습니다

문제

We've come up with a new encryption method we call Leapfrog Encryption. It is a key-based encryption scheme where an alphabetic key specifies how letters in the plaintext (the text to be encrypted) are placed in the ciphertext (the resulting encrypted string). Here's how Leapfrog Encryption works:

  1. Remove all non-alphabetic characters from the plaintext and convert all remaining letters to lowercase.
  2. Convert the letters of the key to their location in the alphabet +1+ 1 (so that 'a' converts to 22, 'b' converts to 33 and so on). This gives us a sequence of numbers d_1,d_2,…,d_nd\_1, d\_2, \ldots, d\_n where nn is the length of the key.
  3. Going left-to-right, place the first letters in the plaintext in every d_1d\_1-th location of the ciphertext until you run out of positions in the ciphertext (where the length of the ciphertext is equal to the number of letters in the plaintext). So for example, if d_1=5d\_1=5 the first letter in the plaintext goes in position 55 of the ciphertext (numbering the first location in the ciphertext as position 11), the second letter in the plaintext goes in position 1010 in the ciphertext, and so on.
  4. Repeat this with d_2d\_2 but this time going right-to-left through the ciphertext, only counting the empty positions (leapfrogging over the letters already in the ciphertext).
  5. Continue with d_3d\_3, d_4d\_4, etc., alternating the direction you go through the ciphertext each time.
  6. If there are still letters left in the plaintext after using d_nd\_n, fill in the remaining empty locations in the ciphertext with these remaining letters, again going in the opposite direction of the previous pass (this is equivalent to having d_n+1=1d\_{n+1} = 1).

For example, if our plaintext is "Send more monkeys!" and our key is "bea", the encryption proceeds as follows:

b →\rightarrow 3, left-to-right:_ _ s _ _ e _ _ n _ _ d _ _ m
e →\rightarrow 6, right-to-left:_ _ s _ _ e o _ n _ _ d _ _ m
a →\rightarrow 2, left-to-right:_ r s _ e e o _ n m _ d o _ m
last pass, right-to-left:s r s y e e o e n m k d o n m

Decryption is done by …\ldots hey, you know what? We're going to let you figure that out.

입력

The first line of input contains two strings tt kk where tt is either E or D indicating whether to perform encryption or decryption and kk is the lowercase alphabetic key. The length of kk will be between 11 and 100100, inclusive. The second and final line of input contains the plaintext to encrypt (if tt is E) or the ciphertext to decrypt (if tt is D). This string is non-empty and has a maximum length of 2,0002\\,000. A ciphertext string consists of lowercase letters only, while a plaintext string may contain uppercase letters, numbers, punctuation and spaces as well (all counted as part of the length of the string) and is guaranteed to contain at least one letter.

출력

Output the encrypted or decrypted text. Your output should only contain lowercase letters.

예제3

  1. 예제 1

    입력
    E bea
    Send more monkeys!
    
    예상 출력
    srsyeeoenmkdonm
    
  2. 예제 2

    입력
    D bea
    srsyeeoenmkdonm
    
    예상 출력
    sendmoremonkeys
    
  3. 예제 3

    입력
    D zyxwvutsrqponmlkjihgfedcba
    lafogrpe
    
    예상 출력
    leapfrog