← 返回 uber 的题目列表Text Justification
类型:online_judge
Text Justification
Given an array of non-empty words words and an integer maxWidth, format the text so that every output line contains exactly maxWidth characters.
Rules:
Pack as many words as possible into each line, with at least one space between adjacent words.
Except for the last line, every line must be fully justified. Distribute extra spaces as evenly as possible among gaps; if they cannot be distributed evenly, gaps on the left receive more spaces.
The last line is left-justified: use one space between words and pad trailing spaces to maxWidth.
A line containing only one word is left-justified and padded on the right.
Input Format
First line: integer maxWidth
Second line: words separated by single spaces
Output Format
Print the formatted lines, each with length maxWidth.
Constraints
1 <= words.length <= 300
1 <= words[i].length <= maxWidth <= 100
Words contain no whitespace.
Example
Input:
16
This is an example of text justification.
Output:
This is an
example of text
justification.
Example
Input
16
This is an example of text justification.
Output
This is an
example of text
justification.