PivotOJ

Leapfrog Encryption

시간 제한: 1000ms메모리 제한: 2048MB출처: ICPC Greater New York 2024BOJ 32810

문제

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 d1,d2,,dnd_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 d1d_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 d1=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 d2d_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 d3d_3, d4d_4, etc., alternating the direction you go through the ciphertext each time.
  6. If there are still letters left in the plaintext after using dnd_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 dn+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 20002\,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.

예제

예제 1

입력
E bea
Send more monkeys!
출력
srsyeeoenmkdonm

예제 2

입력
D bea
srsyeeoenmkdonm
출력
sendmoremonkeys

예제 3

입력
D zyxwvutsrqponmlkjihgfedcba
lafogrpe
출력
leapfrog
코드를 제출하려면 로그인하세요.