A Very Nasty Text Formatter |
This problem deals with pretty-printing of an arbitrary text. You are given a text (see also EBNF below) composed of some paragraphs. Each paragraph is initiated by an integer n. You are expected to output this paragraph in such a way that every row is <CR>-terminated and is except for the last one exactly n characters long (<CR> not counted).
This justified output should be done by inserting some extra spaces between the words of a row. In some cases, you may be forced to hyphenate a word to achieve justified output. Do this hyphenation where you want because you do not have to follow any linguistic rules. You should do as few hyphenations as possible, however. The assumptions that you can make are
Note: The text is built following this EBNF
text = paragraph {paragraph} <eof>. paragraph = n " " word {" " word} <CR>. n = digit {digit}. word = letter {letter} [mark]. digit = "0" | "1" | "2" | ... | "8" | "9". letter = "A" | "B" | ... | "Y" | "Z" | "a" | "b" | ... | "y" | "z". mark = "." | "," | ";" | ":".
10 This is an example of a paragraph which is prettyprinted on a row with a length of ten.<CR> <eof>
This is an<CR> example of<CR> a paragra-<CR> ph which<CR> is pretty-<CR> printed on<CR> a row with<CR> a length<CR> of ten.<CR> <eof>