算法导论第三版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)}
只写思路,懒得上代码了...
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)}
只写思路,懒得上代码了...
相关文章推荐
- 书评:《算法之美( Algorithms to Live By )》
- 动易2006序列号破解算法公布
- Ruby实现的矩阵连乘算法
- C#插入法排序算法实例分析
- 超大数据量存储常用数据库分表分库算法总结
- C#数据结构与算法揭秘二
- C#冒泡法排序算法实例分析
- 算法练习之从String.indexOf的模拟实现开始
- C#算法之关于大牛生小牛的问题
- C#实现的算24点游戏算法实例分析
- c语言实现的带通配符匹配算法
- 浅析STL中的常用算法
- 算法之排列算法与组合算法详解
- C++实现一维向量旋转算法
- Ruby实现的合并排序算法
- C#折半插入排序算法实现方法
- 基于C++实现的各种内部排序算法汇总
- C++线性时间的排序算法分析
- C++实现汉诺塔算法经典实例
- PHP实现克鲁斯卡尔算法实例解析