Word Wrap Cypher

View as PDF

Submit solution

Points: 7
Time limit: 2.5s
Memory limit: 16M

Author:
Problem type
ECOO Practice 2014

In text display, word wrap is the feature of moving text to a new line when the current line is full, so that all text is visible without having to scroll horizontally. This makes it so that newlines do not have to be hardcoded in a piece of text, and that the text can be dynamically reflowed when the text editor is resized. When a word (more specifically, a consecutive series of characters that does not contain a space) is too long to fit on the current line, the entire word is displayed on the next line, where the remaining text continues. The space that comes before the wrapped word is always on the line above it, meaning that no wrapped line can have a space as the first character. The following diagram illustrates word wrap on a piece of text.

Word Wrap Cypher is a way of encoding a message based on word wrapping a text and right-aligning it. We are given a width W (1 \le W \le 100), along with a single-line message of no more than 1\,000 ASCII characters. To encode the message using Word Wrap Cypher, we follow these instructions:

  1. Word wrap the line of text to a width of W characters.
  2. Fill the end of each line with spaces until it is W characters wide (not including the newline character).
  3. Right align the piece of text, trimming off all trailing spaces and padding each line with leading spaces so that their widths remain W characters long.
  4. Interpret the resulting right-aligned text column by column into a single-lined string. That is, first take the character at row 1 column 1, then at row 2 column 1, etc. After reaching the end of column 1, we take the character at row 1 column 2, then at row 2 column 2, and so on.

It is guaranteed that you will not be dealing with original messages that contain words longer than W characters (and thus cannot be wrapped). Nor will you have to deal with messages that contain 2 or more consecutive spaces. Let's say we want to encode the following message for W = 15.

Every man must die, Jon Snow. But first he must live.

For the purposes of this explanation only, we will use underscores to represent spaces. The text wraps like this:

Every_man_must_
die,_Jon_Snow._
But_first_he___
must_live._____

Right aligning the above, we get:

_Every_man_must
_die,_Jon_Snow.
___But_first_he
_____must_live.

Interpreting the text column by column yields the following encoded string:

____Ed__vi__eeB_r,u_y_tm_J_umofsanitn_r__Sslmntiuo_vswhet.e.

Given an encoded string, your task is to determine the original message.

Input Specification

The input contains 5 test cases with 2 lines per test case. The first of these lines contains a single integer W, and the second of these lines contains the resulting encoded string.

Output Specification

For each test case, output a line containing the original, decoded message.

Sample Input

15
    Ed  vi  eeB r,u y tm J umofsanitn r  Sslmntiuo vswhet.e.
7
  p  SDr  yoa gr y oiy tdooths:uoe?
10
   A  r tyohale:d   nTaehnwed.
25
   S  y  r  i  og :o  d T, h  ea rn ed    ih si  s o  nn ladyme eao tnihes.
30
      St yh ri in og :   w Ae n  ds  a ty h  et ro e   d ie sa  t oh n. l.ty.o  doNanoyet.

Sample Output

Every man must die, Jon Snow. But first he must live.
Syrio: Do you pray to the gods?
Arya: The old and the new.
Syrio: There is only one god, and his name is death.
Syrio: And there is only one thing we say to death... Not today.

Comments

There are no comments at the moment.