Repeated Permutation Cipher
InterviewTime limit1sMemory limit128 MB
Given a permutation key and a repeat count k, apply the permutation to each padded message k times and print the result.
- Level
Medium5 of 10
- Topics
- Array, Implementation, Math, Simulation
- Solved
- No attempts yet
Problem
Bob and Alice began using a brand-new encryption scheme. It is not a public-key cryptosystem; instead it relies on a secret key known only to the two of them.
The secret key is a sequence of distinct integers , each greater than and at most . In other words, is a permutation of through .
Encryption works as follows. Write the message under the key so that each character of the message lines up with a number of the key. The character at position in the message is moved to position in the encrypted message. The resulting text is then encrypted again in the same way, and this is repeated a total of times. After the -th encryption the result is exchanged.
The length of the message is always at most . If the message is shorter than , spaces are appended to its end so that its length becomes exactly .
Given the key and several pairs of a repeat count and a message, write a program that outputs each message after it has been encrypted times.
Input
The input consists of several blocks.
The first line of each block contains an integer (). The next line contains the key: integers, pairwise distinct and each between and , separated by spaces. The following lines each contain an integer and a message of ASCII characters, separated by a single space. Each line ends with an end-of-line character, which is not part of the message. A block ends with a line containing only the number .
After the last block, a separate line containing only the number marks the end of the input.
may be very large.
Output
For each (repeat count, message) pair, print the encrypted message on its own line. Every printed line has exactly characters, preserving any trailing spaces of the padded message. The results of the blocks are printed in the order they appear in the input, with no blank line between blocks.