TOANDFRO - To and Fro

Mo and Larry have devised a way of encrypting messages. They first decide secretly on the number of columns and write the message (letters only) down the columns, padding with extra random letters so as to make a rectangular array of letters. For example, if the message is “There’s no place like home on a snowy night” and there are five columns, Mo would write 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

Note that Mo includes only letters and writes them all in lower case. In this example, Mo used the character ‘x’ to pad the message out to make a rectangle, although he could have used any letter. Mo then sends the message to Larry by writing the letters in each row, alternating left-to-right and right-to-left. So, the above would be encrypted as


Your job is to recover for Larry the original message (along with any extra padding letters) from the encrypted one.


There will be multiple input sets. Input for each set will consist of two lines. The first line will contain an integer in the range 2...20 indicating the number of columns used. The next line is a string of up to 200 lower case letters. The last input set is followed by a line containing a single 0, indicating end of input.


Each input set should generate one line of output, giving the original plaintext message, with no spaces.




hide comments
tejaswini_526: 2023-10-19 14:16:00

What does AC mean?

mohit_010: 2021-11-12 08:53:13


[NG]: Quit posting code / solutions / spoilers or you'll see your commenting privilege removed.

Last edit: 2021-11-12 17:48:39
sri_nidhi5: 2021-09-09 20:37:39

using 2D array it will be O(m*n) right !! is there any O(n) logic?

shankar_0x: 2021-06-01 06:38:59


Last edit: 2021-06-01 06:51:01
svxvii: 2021-06-01 06:36:52

AC in one go! Use 2D arrays

Last edit: 2021-06-01 06:50:47
utkarshag_20: 2021-03-31 10:57:13

AC in 1 go.

yasser1110: 2021-02-14 08:52:46

This can be solved without using any extra array. If you need help, please ask on discuss.

Last edit: 2021-02-14 08:53:41
osamanagi: 2021-02-08 18:10:32

no one give a damn about your first go...

Last edit: 2021-02-08 18:12:42
makhan_28: 2020-12-12 13:42:08

Many people here are trying 2-D array or Matrix approach though its nothing wrong with it but you all should try to see pattern and solve it in 1-D array, I think thats the motive of problem to guess pattern and run a correct loop though its hard way to solve it like this ......

priyanshnaskar: 2020-11-25 13:16:09

I don't know what's happening but my answer according to test cases is correct but on submitting it's saying the wrong answer

Added by:Adrian Kuegel
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Resource: ACM East Central North America Regional Programming Contest 2004