To and Fro
InterviewTime limit1sMemory limit128 MB
Decode a message written row by row in alternating left-to-right and right-to-left order back into column-major plaintext.
- Level
Easy3 of 10
- Topics
- String, Simulation, Implementation, Array
- Solved
- No attempts yet
Problem
Mo and Larry have devised a way of encrypting messages. They first decide secretly on the number of columns, then write the message (letters only) down the columns, padding with extra random letters so that the letters form a rectangular array.
For example, if the message is "There's no place like home on a snowy night" and there are five columns, Mo writes down:
t o i o y
h p k n n
e l e a i
r a h s g
e c o n h
s e m o t
n l e w x
Mo includes only letters and writes them all in lower case. In this example Mo used 'x' to pad the message out into a rectangle, although any letter could have been used.
Mo then sends the message to Larry by writing the letters in each row, alternating left-to-right and right-to-left. So the array above is encrypted as:
toioynnkpheleaigshareconhtomesnlewx
Your task is to recover, for Larry, the original message (including any extra padding letters) from the encrypted one.
Input
There are multiple input sets. The input for each set consists of two lines. The first line contains an integer in the range 2 to 20 giving the number of columns used. The second line is a string of up to 200 lower-case letters; its length is always a multiple of the number of columns (it forms a rectangle). The last input set is followed by a line containing a single 0, marking the end of input.
Output
For each input set, print one line giving the original plaintext message, with no spaces.