String Decoding
InterviewTime limit1sMemory limit128 MB
Given a string, a permutation, and a large repetition count m, recover the string that the permutation maps to the given encoded string.
- Level
Medium7 of 10
- Topics
- Math, Implementation, String, Simulation
- Solved
- No attempts yet
Problem
There is a method for encoding a string, described below.
Let the characters of the string to be encoded be in order. Encoding follows this procedure.
- Choose a natural number and a permutation made of the distinct numbers of the set .
- Repeat step 3 below exactly times.
- For every , set , then replace each with . (That is, in one step the -th character of the new string becomes the -th character of the previous string.)
For example, encoding the string "hello" with and the permutation transforms it as follows.
"hello" → "elhol" → "lhelo" → "helol"
Given the encoded string together with the and the permutation used for encoding, write a program that recovers (decodes) the original string from before encoding.
Input
The input consists of several test cases.
The first line of each test case contains two integers and . (, )
The second line contains the distinct integers used for encoding. ()
The third line contains the encoded string. Its length is , and it may contain spaces.
The last line of the input contains two zeros, and this line is not processed.
Output
For each test case, print the decoded original string on its own line.