A Very Nasty Text Formatter Input and Output
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 -terminated and is except for the last one exactly n characters long ( not counted).
This justified output should be done by inserting some extra spaces between the words of a row. Divide the spaces left as equally as possible among the spaces between words. In case you can’t divide them equally, the leftmost spaces between words should be longer than the rightmost ones.
In some cases, you may be forced to hyphenate a word to achieve justified output. Do this hyphen- ation where you want because you do not have to follow any linguistic rules. You should do as few hyphenations as possible. However, if it is impossible to justify a line of the text, output it left-justified.
The assumptions that you can make are:
- The text follows the EBNF
- 3<n<1000
- There are now words longer than thousand characters
Note 1: The text is built following this EBNF
text = paragraph {paragraph} .
paragraph = n " " word {" " word} .
n = digit {digit}.
word = letter {letter} [mark].
digit = "0" | "1" | "2" | ... | "8" | "9".
letter = "A" | "B" | ... | "Y" | "Z" | "a" | "b" | ... | "y" | "z".
mark = "." | "," | ";" | ":".
Note 2: The explicit ’s and ’s have been added on the sample input and output to make it readable. They won’t exist on the input and output files.
Sample Input
20 This is an example of a paragraph which is prettyprinted on a row with a
length of twenty.
15 This is an example of a paragraph which is prettyprinted on a row with a
length of fifteen.
10 This is an example of a paragraph which is prettyprinted on a row with a
length of ten.
5 This is an example of a paragraph which is prettyprinted on a row with a
length of five.
289 – A Very Nasty Text Formatter 2/2 Sample Output
This is an example
of a paragraph which
is prettyprinted on
a row with a length
of twenty.
This is an
example of a
paragraph which
is prettyprint-
ed on a row
with a length
of fifteen.
This is an
example of
a paragra-
ph which
is pretty-
printed on
a row with
a length
of ten.
This
is an
exam-
ple
of a
para-
graph
which
is p-
rett-
ypri-
nted
on a
row
with
a le-
ngth
of f-
ive.