PersonaOA #1
Text Justification with Hyphens
估计困难评测数据准备中,可先阅读题面
考点(AI 标注)字符串模拟/实现
Given a list of words and a maximum line length, format the text so each line is exactly that length. Rules:
Greedy packing: pack as many words as possible onto each line (separated by at least one space).
Extra spaces in multi-word lines: distribute extra spaces evenly between words; if they don't divide evenly, the leftmost gaps get one extra each. Output uses _ instead of space.
Single-word lines: center the word (pad // 2 underscores on the left, the rest on the right). If pad is odd, the extra goes to the right.
Hyphenated words: words like "self-inflicted", "pre-approval", "mother-in-law" should be kept on one line if possible. If only the prefix-with-hyphen fits (e.g. "self-"), split the word across lines. Inputs may have whitespace around hyphens ("self - inflicted"); normalize them as if there were no whitespace.
Example
words = "auto-complete is my go - to", lineLength = 8
→ ["_auto-__", "complete", "is____my", "_go-to__"]
words = "cat is an animal and so is a dog", lineLength = 12
→ ["cat__is__an_", "animal__and_", "so_is_a_dog_"]
words = "human", lineLength = 8
→ "_human__"
Constraints
1 ≤ lineLength ≤ 100, total word characters fit within reasonable memory.