Progressive Scramble
InterviewTime limit2sMemory limit512 MB
Encrypt a message by cumulative sums of symbol values mod 27, and decrypt by inverting the same recurrence.
- Level
Easy3 of 10
- Topics
- Implementation, Math, String, Prefix sum
- Solved
- No attempts yet
Problem
You belong to a naive spy agency. To keep its traffic private, the agency uses a very simple encryption algorithm that changes each symbol of a message progressively, based on the symbols before it. The allowed symbols are the space and the 26 lowercase English letters. For encryption they are given the values 0 (for a space) and 1 through 26 (for a to z). Write for the numeric value of symbol .
Consider a message with symbols . The algorithm first turns the leading symbol into its value . Then, for every following symbol , it computes , the sum of that symbol's value and the computed value of the previous symbol. (When the message holds a space, the previously scrambled letter appears once more.) The process repeats until every is known.
At this point the message is a sequence of numbers. To print it, convert it back to symbols. Since there are 27 valid symbols, take modulo 27 and replace that value with the matching symbol. For example, if , then , and the symbol is e because .
Encrypting the string "my pie" goes like this.
- Turn each symbol into :
[13, 25, 0, 16, 9, 5] - Compute each :
[13, 38, 38, 54, 63, 68] - Take each modulo 27:
[13, 11, 11, 0, 9, 14] - Convert back to symbols: "mkk in"
Write a program that encrypts text with this algorithm and also decrypts text that was encrypted with it.
Input
The first line holds one integer (). Each of the next lines holds the letter e or d, a single space, and then a message that runs to the end of the line. A message is made of lowercase English letters and spaces only, and its length is between 1 and 80 characters. The letter e means encrypt the string that follows, and d means decrypt it.
Output
For each message in the input, print the result of encrypting or decrypting it on its own line. Differences in whitespace matter in this problem. Your output must match the correct output character for character, spaces included.