Problem B. Zigzag Cipher
Xem dạng PDFTrong trường hợp đề bài hiển thị không chính xác, bạn có thể tải đề bài tại đây: Đề bài
During a joint cybersecurity exercise at UMT University, two student agents-codenamed An and Khang-communicate through an unusual zigzag columnar transposition. Given a positive integer M (the number of columns), the sender writes the plaintext into a rectangle row by row:
• Even-numbered rows (0, 2, 4, . . . ) are filled left → right.
• Odd-numbered rows (1, 3, 5, . . . ) are filled right → left.
If the last row is incomplete, random lowercase letters are appended until the rectangle is full. Finally, the ciphertext is produced by reading the rectangle column by column, top → bottom, left → right.
Your task is to reverse this process: given M and the ciphertext, reconstruct the original rectangle and output the plaintext (including any padding letters exactly as sent).
Input
The input consists of multiple test cases. Each test case contains two lines:
• The 1st line contains a single integer ~M~ ~(2 ≤ M ≤ 20)~ - the number of columns.
• The 2nd line contains a string of lowercase letters (maximum length is 200) - ciphertext.
The input ends with a line containing a single 0. This line should not be processed.
Output
For every test case, print a single line containing the recovered plaintext (padding included, no spaces).
Sample Input 1
5
toioynnkpheleaigshareconhtomesnlewx
3
ttyohhieneesiaabss
0
Sample Ouput 1
theresnoplacelikehomeonasnowynightx
thisistheeasyoneab
Bình luận