#K9516. Fully Justified Text Formatting
Fully Justified Text Formatting
Fully Justified Text Formatting
You are given a list of words and a maximum line width k. Your task is to format the text such that each line has exactly k characters and is fully (left and right) justified. If a word does not fit on the current line, move it to the next line.
For lines other than the last one, extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line does not divide evenly between words, the extra spaces are placed from left to right. The last line should be left-justified and no extra space is inserted between words.
Mathematically, if a line contains words w1, w2, ..., wn then the number of spaces to distribute is given by:
$$\text{TotalSpaces} = k - \sum_{i=1}^{n} \text{len}(w_i)$$
These spaces are distributed between the n - 1 gaps as equally as possible.
inputFormat
The input is given via standard input and has the following format:
- The first line contains a single integer n — the number of words.
- The next n lines each contain a word.
- The last line contains an integer k — the maximum width of each line.
Note that words do not contain spaces.
outputFormat
Output the fully justified text, with each line of exactly k characters. Lines should be printed in order and each line is terminated with a newline.
## sample7
This
is
an
example
of
text
justification.
16
This is an
example of text
justification.
</p>