之前,你認爲它是重複的(也有很多問題,問如何分割長字符串不打破的話)採取記住,我的問題是有點不同:順序並不重要,我爲了儘可能多地使用每一行,我們已經適應了這些詞彙。分割長字符串,不破的話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