Given a width and a block of
text containing possible hyphen-
ation points, format it fully-
justified (in monospace).
有充分理由意味着它是在左对齐和右,和通过增加字间距直到每一行配合来实现的。
有关:
- 通过添加空格来对齐文本
- 将文本对齐块
- 从某种意义上说,这可以被认为是“ 文本处理#1:断字”(似乎从未发布过)的下一步。
输入项
您可以采用任何喜欢的格式输入。您将获得:
- 目标宽度(以字符为单位),范围为5-100(含);
- 包含可能带有连字符的单词的文本块。这可以是用空格分隔的字符串,单词数组或单词片段数组的数组(或所需的任何其他数据表示形式)。
典型的输入可能是:
Width: 25
Text: There's no bu-si-ne-ss lik-e s-h-o-w busine-ss, n-o bus-iness I know.
连字符表示可能的连字符点,空格表示单词边界。文本的可能替代表示形式:
[["There's"], ["no"], ["bu", "si", "ne", "ss"], ["lik", "e"], (etc.)]
输出量
输入文本,在单词之间添加空格,在列宽处添加换行符,并选择连字符点以使其完全与列宽对齐。对于函数,可以返回字符串数组(每行一个),而不使用换行符分隔。
上述输入的可能输出可能是:
There's no business like
show business, no bus-
iness I know.
请注意,除最后一个“ bus-iness”中的连字符外,所有连字符均已删除,该连字符始终显示该单词换行到下一行,并被选择以确保第二行包含尽可能多的文本。
规则
在每一行中,单词之间的空格数不能超过1,但是在其他地方插入多余的空格取决于您:
hello hi foo bar <-- not permitted (1,1,5) hello hi foo bar <-- not permitted (2,1,4) hello hi foo bar <-- OK (2,2,3) hello hi foo bar <-- OK (2,3,2) hello hi foo bar <-- OK (3,2,2)
任何行都不能以空格开头或结尾(最后一行除外,后者可以以空格结尾)。
最后一行应对齐,每个单词之间应包含单个空格。如果需要,可以在其后跟随任意空格/换行符,但这不是必需的。
单词将由AZ,az,0-9和简单标点符号(
.,'()&
)组成您可以假设没有一个单词片段长于目标宽度,并且始终可以根据规则填充行(即,每行上至少有2个单词片段,或1个单词片段可以填充该行)完美)
您必须选择连字符点,以使前几行中的单词字符数量最大化(例如,单词必须由行贪婪地消耗),例如:
This is an input stri-ng with hyph-en-at-ion poi-nts. This is an input stri- <-- not permitted ng with hyphenation points. This is an input string with hyph- <-- not permitted enation points. This is an input string with hyphen- <-- OK ation points.
以字节为单位的最短代码获胜
例子
Width: 20
Text: The q-uick brown fox ju-mp-s ove-r t-h-e lazy dog.
The quick brown fox
jumps over the lazy
dog.
Width: 32
Text: Given a width and a block of text cont-ain-ing pos-sible hyphen-ation points, for-mat it ful-ly-just-ified (in mono-space).
Given a width and a block of
text containing possible hyphen-
ation points, format it fully-
justified (in monospace).
Width: 80
Text: Pro-gram-ming Puz-zles & Code Golf is a ques-tion and ans-wer site for pro-gram-ming puz-zle enth-usi-asts and code golf-ers. It's built and run by you as part of the St-ack Exch-ange net-work of Q&A sites. With your help, we're work-ing to-g-et-her to build a lib-rary of pro-gram-ming puz-zles and their sol-ut-ions.
Programming Puzzles & Code Golf is a question and answer site for programming
puzzle enthusiasts and code golfers. It's built and run by you as part of the
Stack Exchange network of Q&A sites. With your help, we're working together to
build a library of programming puzzles and their solutions.
Width: 20
Text: Pro-gram-ming Puz-zles & Code Golf is a ques-tion and ans-wer site for pro-gram-ming puz-zle enth-usi-asts and code golf-ers. It's built and run by you as part of the St-ack Exch-ange net-work of Q&A sites. With your help, we're work-ing to-g-et-her to build a lib-rary of pro-gram-ming puz-zles and their sol-ut-ions.
Programming Puzzles
& Code Golf is a
question and answer
site for programming
puzzle enthusiasts
and code golfers.
It's built and run
by you as part of
the Stack Exchange
network of Q&A
sites. With your
help, we're working
together to build a
library of program-
ming puzzles and
their solutions.
Width: 5
Text: a b c d e f g h i j k l mm nn oo p-p qq rr ss t u vv ww x yy z
a b c
d e f
g h i
j k l
mm nn
oo pp
qq rr
ss t
u vv
ww x
yy z
Width: 10
Text: It's the bl-ack be-ast of Araghhhhh-hhh-h-hhh-h-h-h-hh!
It's the
black be-
ast of
Araghhhhh-
hhhhhhhhh-
hhh!
anybod-y
宽度为7)结束,我们可以选择输出anybody
还是anybod-\ny
?