之前,你认为它是重复的(也有很多问题,问如何分割长字符串不打破的话)采取记住,我的问题是有点不同:顺序并不重要,我为了尽可能多地使用每一行,我们已经适应了这些词汇。分割长字符串,不破的话fullfilling线
嗨,
我有一个无序的话,我想他们不使用超过253个字符组合。
def compose(words):
result = " ".join(words)
if len(result) > 253:
pass # this should not happen!
return result
我的问题是,我想尝试尽可能地填满线。例如:
words = "a bc def ghil mno pq r st uv"
limit = 5 # max 5 characters
# This is good because it's the shortest possible list,
# but I don't know how could I get it
# Note: order is not important
good = ["a def", "bc pq", "ghil", "mno r", "st uv"]
# This is bad because len(bad) > len(good)
# even if the limit of 5 characters is respected
# This is equivalent to:
# bad = ["a bc", "def", "ghil", "mno", "pq r", "st uv"]
import textwrap
bad = textwrap.wrap(words, limit)
我该怎么办?
这是一个动态规划问题;以与攻击[硬币更换问题]相同的方式攻击它(http://www.geeksforgeeks.org/dynamic-programming-set-7-coin-change/)。 – 2013-05-07 09:00:04