Cyclic Rotation Cipher
Time limit1sMemory limit32 MB
Reconstruct the original lowercase string from its Burrows-Wheeler transform index i and last column R.
Problem
For some time the police have been intercepting encrypted messages from a criminal group, but they have been unable to decrypt them. During a recent raid of an abandoned warehouse they seized the encryption device, and careful analysis revealed how it works.
The device takes plain text as input. The text is first converted to lowercase and stripped of everything except the Latin letters a … z, producing a string . Then all cyclic rotations of (call them , where ) are sorted lexicographically. The encrypted message consists of the index of the original string among the sorted rotations, together with a string formed by taking the last letter of every rotation, read in sorted order.
For example, abracadabra is encoded as 3 rdarcaaaabb:
1. aabracadabr = S11
2. abraabracad = S8
3. abracadabra = S1
4. acadabraabr = S4
5. adabraabrac = S6
6. braabracada = S9
7. bracadabraa = S2
8. cadabraabra = S5
9. dabraabraca = S7
10. raabracadab = S10
11. racadabraab = S3
The sorted rotations are numbered to ; the third one is the original string, so , and reading the last letter of each rotation from top to bottom gives rdarcaaaabb.
Given an encrypted message , recover the original string . The messages can be very long, so your program must be efficient.
Input
The first line contains the index (). The second line contains the string of length (). The original message is guaranteed to exist and to be unique.
Output
Print the string on a single line.