Leapfrog Encryption
Time limit1sMemory limit2048 MB
Given a key and a text, the task encrypts plaintext or decrypts ciphertext by alternately filling empty cells at key-defined skip intervals.
- Level
Medium4 of 10
- Topics
- Implementation, Simulation, String
- Solved
- No attempts yet
Problem
We have come up with a new encryption method called Leapfrog Encryption. It is a key-based 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). Leapfrog Encryption works as follows:
- Remove all non-alphabetic characters from the plaintext and convert all remaining letters to lowercase.
- Convert each letter of the key to its position in the alphabet ('a' becomes 2, 'b' becomes 3, and so on). This gives a sequence of numbers , where is the length of the key.
- Going left-to-right, place the first letters of the plaintext in every -th position of the ciphertext until you run out of positions. The length of the ciphertext equals the number of letters in the plaintext. For example, if , the first letter of the plaintext goes in position 5 of the ciphertext (counting the first position as 1), the second letter goes in position 10, and so on.
- Repeat this with , but going right-to-left through the ciphertext. Only count the empty positions, so you leapfrog over letters already placed.
- Continue with , , and so on, alternating the direction each time.
- If letters remain in the plaintext after using , fill the remaining empty positions with these letters, going in the opposite direction of the previous pass (this is equivalent to ).
For example, take the plaintext "Send more monkeys!" and the key "bea". The encryption proceeds as follows:
Decryption is left for you to figure out.
Input
The first line contains two strings and . is either E or D, meaning encrypt or decrypt. is the lowercase alphabetic key, with length between 1 and 100, inclusive.
The second and final line contains the plaintext to encrypt (if is E) or the ciphertext to decrypt (if is D). The string is non-empty and has at most 2,000 characters. A ciphertext consists of lowercase letters only. A plaintext may also contain uppercase letters, numbers, punctuation, and spaces, all of which count toward the length. A plaintext is guaranteed to contain at least one letter.
Output
Output the encrypted or decrypted text. The output contains only lowercase letters.