您的位置:首页 > 其它

算法导论第三版15-4整齐打印Printing neatly

2016-02-09 22:56 936 查看
Consider the problem of neatly printing a paragraph with a monospaced font (all characters
having the same width) on a printer. The input text is a sequence of n words of lengths
l1; l2; ···; ln,
measured in characters. We want to print this paragraph neatly on a number of lines that hold a maximum of
M characters each. Our criterion of “neatness” is
as follows. If a given line contains words i
through j
, where
i
j , and we leave exactly one space between words, the number of extra space characters
at the end of the line is M-j+i-sum{lk(i<=k<=j)},
which must be nonnegative so that the words fit on the line. We wish to minimize the sum,
over all lines except the last, of the cubes of the numbers of extra space characters at the ends
of lines. Give a dynamic-programming algorithm to print a paragraph of n words neatly on a printer.
Analyze the running time and space requirements of your algorithm.

s[n+1][n+1]数组,s[i][j]记录l[i]到l[j]在一行时,到打印完这一行时已经有的立方和最小值

v(i, j) = (M-j+i-sum(lk))^3(j<n)

v(i, j) = 0(j=n)

即s[i][j] = v(i, j)+min{s[t, i-1] (1<=t<=i-1)}

只写思路,懒得上代码了...
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  算法 算法导论