Inverse Move-to-Front Transform
InterviewTime limit2sMemory limit256 MB
Reconstruct the lowercase string from its move-to-front code by simulating the 26-letter list.
- Level
Easy2 of 10
- Topics
- Simulation, Array
- Solved
- No attempts yet
Problem
The Move-to-Front (MTF) transform is an encoding scheme that maps input data to a sequence of numbers. Entropy encoders often reach a better compression ratio on data that has passed through the MTF transform. The transform itself is simple. The scheme below is the MTF transform on a string made of lowercase letters only.
- Keep a list of the lowercase letters. The list starts in lexicographic order, so at the beginning it is [abcdefghijklmnopqrstuvwxyz].
- Read one character from the string. Output the index of in the list, then move to the front of the list.
- Repeat step 2 until every character of the string has been read.
Applying the transform to the string hakka goes like this.
- The first character h has index 7 in [abcdefghijklmnopqrstuvwxyz]. Output 7, then move h to the front.
- The second character a has index 1 in [habcdefgijklmnopqrstuvwxyz]. Output 1, then move a to the front.
- The third character k has index 10 in [ahbcdefgijklmnopqrstuvwxyz]. Output 10, then move k to the front.
- The fourth character k has index 0 in [kahbcdefgijlmnopqrstuvwxyz]. Output 0, then move k to the front.
- The fifth character a has index 1 in [kahbcdefgijlmnopqrstuvwxyz]. Output 1, then move a to the front.
So the MTF transform maps hakka to the sequence .
Write a program that inverts the MTF transform. Given a sequence , compute the string that the MTF transform maps to .
Input
The first line contains an integer , the number of test cases, with .
Each test case consists of two lines. The first line contains a positive integer , the length of the sequence, with . The second line contains the integers separated by blanks, with for every .
Output
For each test case, print on its own line the string that the MTF transform maps to .