W's Cipher
InterviewTime limit1sMemory limit128 MB
Decrypt messages by extracting each of three character groups and rotating its subsequence right by the matching key.
- Level
Easy3 of 10
- Topics
- String, Implementation, Simulation
- Solved
- No attempts yet
Problem
A family of memory-constrained wireless devices is too small to run standard encryption, so it uses the following lightweight cipher instead. Implement it.
Encryption uses three integer keys , , . The characters are split into three groups: the letters [a-i] form the first group, [j-r] the second, and everything else ([s-z] and the underscore _) the third. Take only the characters of one group, in the left-to-right order they appear in the message: encryption rotates this subsequence left by positions and writes the rotated characters back into the same positions. Each group is rotated independently, and rotating one group never moves any character of another group.
Decryption reverses this: within each group, rotate the subsequence right by positions.
For example, the message the_quick_brown_fox encrypted with keys , , becomes _icuo_bfnwhoq_kxert. To decrypt, look at the [a-i] characters of the ciphertext. They are i, c, b, f, h, e, occupying (1-indexed) positions 2, 3, 7, 8, 11, 17. A right rotation by puts h, e, i, c, b, f into those positions. The table below shows the message after decrypting each group in turn.
Given the keys and a ciphertext, output the decrypted message.
Input
Every string consists only of lowercase letters and underscores (_) and is at most 80 characters long. Each is an integer between 1 and 100 inclusive.
The input contains one or more encrypted messages. Each message is given as two lines: the first line holds the three keys , , separated by spaces, and the second line is the encrypted message. The input ends with a line 0 0 0 (all keys zero), which is not processed.
Output
For each encrypted message, print the decrypted string on its own line.