Secret Letter
InterviewTime limit1sMemory limit128 MB
Given a ciphertext, undo a 90-degree clockwise rotation of a square grid to recover the original letter.
- Level
Easy3 of 10
- Topics
- Implementation, Array, String, Math
- Solved
- No attempts yet
Problem
Seonyeong wrote a love letter to her boyfriend. To keep others from reading it, she encrypts the letter as follows.
- Write the letter's characters into a square grid, one character at a time, in row-major order (left to right, top to bottom), starting at the top-left cell.
- Rotate this square 90 degrees clockwise.
- Read the rotated square again in row-major order, from the first row to the last row, and concatenate everything into a single line. The resulting string is the ciphertext.
Seonyeong never uses spaces, so the length of the letter is always a perfect square.
For example, if the original letter is RosesAreRedVioletsAreBlue (length 25, i.e. a 5×5 square), the encrypted letter becomes eedARBtVrolsiesuAoReerles.
Given an encrypted letter, write a program that restores the original letter.
Input
The first line contains the number of test cases . is at most .
Each of the next lines contains one encrypted letter. Each letter consists only of uppercase and lowercase English letters, and its length is at least , at most , and always a perfect square.
Output
For each test case, print the restored original letter on its own line.