dp-整齐打印
整齐打印
算法导论3rd - 15.4
问题描述
考虑在一个打印机上整齐地打印一段文章的问题。输入的正文是n个长度分别为L1、L2、……、Ln(以字符个数度量)的单词构成的序列。我们希望将这个段落在一些行上整齐地打印出来,每行至多M个字符。“整齐度”的标准如下:如果某一行包含从i到j的单词(i<j),且单词之间只留一个空格,则在行末多余的空格字符个数为 M - (j-i) - (Li+ …… + Lj),它必须是非负值才能让该行容纳这些单词。我们希望所有行(除最后一行)的行末多余空格字符个数的立方和最小。请给出一个动态规划的算法,来在打印机整齐地打印一段又n个单词的文章。分析所给算法的执行时间和空间需求。
分析
用 costs[i,j] 表示子串<i...j>
构成完整的一行或多行的代价。
递推关系:
costs[0,n] = min(
costs[0,0] + costs[1,n],
costs[0,1] + costs[2,n],
costs[0,2] + costs[3,n],
...
costs[0,n-1] + costs[n,n],
)
// 如果子串 <i...j> 构成了一行:
costs[i,j] = (M - sum(li...lj) - (j-i))^3
// 如果子串 <i...j> 构成了多行:(其中各个子串的代价之和是最小代价)
costs[i,j] = costs[i,i1] + costs[i1+1,i2] + ... + costs[ik+1,j]
程序
#include <iostream>
#include <string>
#include <vector>
#include <time.h>
#include <limits.h>
#define M 30
using namespace std;
void solve();
template<typename T>
void print_mat(T** mat, int len1, int len2);
void print_str(vector<string> words, int i, int j);
int pow(int a, unsigned int n);
int main(int argc, char** argv) {
clock_t start, end;
start = clock();
solve();
end = clock();
cout << "time costs: " << (end-start)*1000./CLOCKS_PER_SEC << " ms\n";
return 0;
}
void solve() {
vector<string> words{"Consider", "the", "problem", "of",
"neatly", "printing", "a", "paragraph", "with", "a",
"monospaced", "font", "(all", "characters", "having",
"the", "same", "width).", "The", "input", "text", "is",
"a", "sequence", "of", "n", "words", "of", "lengths",
"l", "1,", "l", "2,", "…,", "ln", ",", "measured", "in",
"characters,", "which", "are", "to", "be", "printed", "neatly",
}; /*
"on", "a", "number", "of", "lines", "that", "hold", "a", "maximum",
"of", "M", "characters", "each.", "No", "word", "exceeds", "the",
"line", "length,", "so", "that", "li", "≤", "M", "for", "i", "=",
"1,", "2,", "…,", "n", ".", "The", "criterion", "of", "“neatness”",
"is", "as", "follows.", "If", "a", "given", "line", "contains",
"words", "i", "through", "j", ",", "where", "i", "≤", "j", ",",
"and", "exactly", "one", "space", "appears", "between", "words,",
"then", "the", "number", "of", "extra", "space", "characters",
"at", "the", "end", "of", "the", "line", "is", ",", "which", "must",
"be", "nonnegative", "so", "that", "the", "words", "fit", "on", "the",
"line.", "The", "goal", "is", "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.", "Analyze", "the",
"running", "time", "and", "space", "requirements", "of", "your",
"algorithm."};
// */
int len = words.size();
int* length = new int[len];
// 代价
int** costs = new int*[len];
// 记录回车位置
int** enter = new int*[len];
// 空格数 spaces[i][j] = M - sum(li...lj) - (j-i)
int** spaces = new int*[len];
for (int i = 0; i < len; ++i) {
length[i] = words[i].length();
costs[i] = new int[len]{0};
enter[i] = new int[len]{0};
spaces[i] = new int[len]{0};
}
for (int diff = 0; diff < len; ++diff) {
for (int i = 0; i < len - diff; ++i) {
int j = i + diff;
for (int k = i; k <= j; ++k) {
spaces[i][j] += length[k];
}
spaces[i][j] = M - spaces[i][j] - (j-i);
if (spaces[i][j] < 0) {
costs[i][j] = INT_MAX;
for (int k = i; k < j; ++k) {
if (costs[i][k] + costs[k+1][j] < costs[i][j]) {
// 最后一行不计入代价
costs[i][j] = costs[i][k] + (j==len-1? 0 : costs[k+1][j]);
enter[i][j] = k;
}
}
} else {
// 最后一行不计入代价
costs[i][j] = j==len-1? 0 : pow(spaces[i][j], 3);
enter[i][j] = j;
}
}
}
cout << "costs: \n";
print_mat(costs, len, len);
cout << "spaces: \n";
print_mat(spaces, len, len);
cout << "enter: \n";
print_mat(enter, len, len);
vector<pair<int, int>> ent{{0, len-1}};
while (!ent.empty()) {
int k = enter[ent[0].first][ent[0].second];
cout << ent[0].first << "," << ent[0].second << ": " << k << endl;
if (k != ent[0].second) {
ent.push_back({ent[0].first, k});
} else {
print_str(words, ent[0].first, ent[0].second);
}
if (k+1 < ent[0].second) {
ent.push_back({k+1, ent[0].second});
}
ent.erase(ent.begin());
}
for (int i = 0; i < len; ++i) {
delete[] costs[i];
delete[] enter[i];
delete[] spaces[i];
}
delete[] length;
delete[] spaces;
delete[] costs;
delete[] enter;
}
template<typename T>
void print_mat(T** mat, int len1, int len2) {
for (int i = 0; i < len1; ++i) {
for (int j = 0; j < len2; ++j) {
cout << mat[i][j] << "\t";
}
cout << endl;
}
}
int pow(int a, unsigned int n) {
if (a < 2) {
return a;
}
int ret = 1;
int multi = 1;
while (n) {
ret *= n & 0x1 ? a : 1;
n >>= 1;
multi = n ? a*a : multi;
a = multi;
}
return ret;
}
void print_str(vector<string> words, int i, int j) {
while (i <= j) {
cout << words[i++];
if (i <= j) {
cout << " ";
}
}
cout << endl;
}
测试:
$ g++ -o print_words print_words.cpp && ./print_words
costs:
10648 5832 1000 343 0 2728 2000 370 125 27 2027 882 397 3027 1394 946 461 3370 2394 1458 973 586 488 2610 2187 1801 1189 802 488 2953 2530 2144 1865 1314 927 829 496 2360 1052 830 712 523 496 1395 0
0 19683 6859 4096 729 0 6293 1729 854 756 1 2241 1366 1001 28 2095 1430 1344 1028 92 1942 1555 1457 1244 821 435 931771 1457 1587 1164 778 499 218 101 93 1465 994 226 94 1681 1210 1021 569 0
0 0 12167 8000 2197 64 8 3197 2322 1792 35 3709 2792 1035 62 3521 2856 1378 1062 126 3368 2981 2709 1278 855 469 127 3197 2007 1621 1198 812 533 252 135 127 1541 1028 260 128 1757 1244 1055 603 8
0 0 0 21952 9261 1728 1000 0 4472 3456 1027 512 27 2027 1024 576 91 2370 2024 1088 603 216 118 2240 1817 1431 819 432 118 2583 2160 1774 1495 944 557 459 126 1990 682 460 342 153 126 1025 0
0 0 0 0 13824 3375 2197 27 6119 4941 2224 539 54 3224 1051 603 118 3567 2051 1115 630 243 145 2267 1844 1458 846 459 145 2610 2187 1801 1522 971 584 486 153 2017 709 487 369 180 153 1052 27
0 0 0 0 0 10648 8000 1000 125 27 3197 1512 637 4197 2024 1366 701 4540 3024 1878 1213 826 728 3240 2607 2221 1429 1042 728 3583 2950 2564 2285 1554 1167 1069 736 2780 1292 1070 952 763 736 1635 125
0 0 0 0 0 0 24389 6859 2744 1728 1 4941 2728 1001 28 3457 2792 1344 1028 92 3304 2917 2675 1244 821 435 933133 1973 1587 1164 778 499 218 101 93 1507 994 226 94 1723 1210 1021 569 1
0 0 0 0 0 0 0 9261 4096 2744 27 6119 3744 1027 54 4473 3808 1370 1054 118 4320 3567 2701 1270 847 461 119 3213 1999 1613 1190 804 525 244 127 119 1533 1020 252 120 1749 1236 1047 595 27
0 0 0 0 0 0 0 0 17576 13824 2197 512 27 3197 1024 576 91 3540 2024 1088 603 216 118 2240 1817 1431 819 432 118 2583 2160 1774 1495 944 557 459 126 1990 682 460 342 153 126 1025 27
0 0 0 0 0 0 0 0 0 24389 5832 2197 512 5572 2709 1241 576 5453 3709 1753 1088 701 603 3481 2482 2096 1304 917 603 3482 2825 2439 2160 1429 1042 944 611 2655 1167 945 827 638 611 1510 512
0 0 0 0 0 0 0 0 0 0 8000 3375 1000 6750 3197 1729 1064 5941 4197 2241 1576 1189 1091 3969 2970 2584 1792 1405 1091 3970 3313 2927 2648 1917 1530 1432 1099 3143 1655 1433 1315 1126 1099 1998 1000
0 0 0 0 0 0 0 0 0 0 0 17576 9261 1000 27 9000 4375 1343 1027 91 4887 3540 2674 1243 820 434 923186 1972 1586 1163 777 498 217 100 92 1506 993 225 93 1722 1209 1020 568 27
0 0 0 0 0 0 0 0 0 0 0 0 17576 3375 512 64 6750 3256 1512 576 91 64 4587 1728 1305 919 307 118 2457 2071 1648 1262 983 432 243 145 1991 1478 368 146 2207 1694 1505 711 64
0 0 0 0 0 0 0 0 0 0 0 0 0 8000 2197 729 64 4941 3197 1241 576 189 91 2969 1970 1584 792 405 91 2970 2313 1927 1648 917 530 432 99 2143 655 433 315 126 99 998 64
0 0 0 0 0 0 0 0 0 0 0 0 0 013824 8000 3375 343 27 5572 3887 2540 1674 243 54 28 3005 2186 972 586 307 179 62 2698 2311 2213 650 371 2436 1650 866 587 398 2779 27
0 0 0 0 0 0 0 0 0 0 0 0 0 0019683 10648 2744 1000 64 6840 4941 4075 1216 793 407 65 4587 1945 1559 1136 750 471 190 73 65 1479 966 198 66 1695 1182 993 541 64
0 0 0 0 0 0 0 0 0 0 0 0 0 000 17576 5832 2744 512 27 0 6119 2240 1241 855 243 54 2969 2241 1584 1198 919 368 179 81 1927 1414 304 82 2143 1630 1441 647 27
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 12167 6859 2197 512 125 27 3925 2926 2240 728 341 273926 2969 2583 1728 853 466 368 35 2457 591 369 251 62 35934 27
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 19683 9261 4096 2197 1331 8 6840 5572 2662 1843 737 351 72 16 3662 2355 1968 1870 415 136 2093 1415 631 352 163 2436 8
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 15625 8000 4913 3375 216 27 1 4706 3887 945 559 280 152 35 4399 3142 2276 623 344 2619 1623 839 560 371 2962 27
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 17576 12167 9261 1728 729 343 1 6641 2457 1729 1072 686 407 126 9 1 1415 902 134 2 1631 1118 929 477 1
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 21952 17576 4913 2744 1728 216 27 4472 3456 2457 2071 1216 341 152 54 2800 1945 277 55 2674 2161 1972 620 27
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 24389 8000 4913 3375 729 216 6119 5103 4104 2926 1729 728 341 243 3457 2458 466 244 3187 2674 2485 809 216
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 10648 6859 4913 1331 512 06641 4706 3528 2331 1024 637 539 8 3060 762 540 224 35 81105 0
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 21952 17576 8000 4913 729 343 64 8 7110 4825 2926 2060 407 128 2403 1407 623 344 155 2746 64
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 24389 12167 8000 1728 1000 343 125 8 5824 3925 3059 686 341 3402 1686 902 557 368 3745 125
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 15625 10648 2744 1728 729 343 64 6840 4941 3925 1072 559 4268 2072 1288 775 586 4269 343
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 21952 8000 5832 3375 2197 1000 125 8 0 2728 1729 133 1 2458 1945 1756 476 0
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 12167 9261 5832 4096 2197 512 125 27 3925 2926 250 28 3655 3142 2953 593 27
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 024389 17576 13824 9261 4096 2197 1331 8 6840 1674 1008 224 35 82017 8
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 0021952 17576 12167 5832 3375 2197 64 1 2540 1064 280 91 28 2883 1
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 000 24389 17576 9261 5832 4096 343 64 4439 1343 559 280 91 3540 64
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 000 0 21952 12167 8000 5832 729 216 4825 1729 945 432 243 3926 216
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 17576 12167 9261 1728 729 5824 2728 1458 945 756 4925 729
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 21952 17576 4913 2744 8 4941 3473 2960 2771 351 8
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 24389 8000 4913 125 7110 5642 5129 3500 468 125
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 10648 6859 343 1 7588 6175 3718 686 1
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 21952 4096 1000 216 27 0 3197 0
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 6859 2197 729 216 27 4394 27
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 15625 9261 5832 3375 343 0
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 19683 13824 9261 2197 0
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 21952 15625 4913 0
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 21952 8000 0
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 12167 0
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 00
spaces:
22 18 10 7 0 -9 -11 -21 -26 -28 -39 -44 -49 -60 -67 -71 -76 -84 -88 -94 -99 -102 -104 -113 -116 -118 -124 -127 -135 -137 -140 -142 -145 -150 -153 -155 -164 -167 -179 -185 -189 -192 -195 -203 -210
0 27 19 16 9 0 -2 -12 -17 -19 -30 -35 -40 -51 -58 -62 -67 -75 -79 -85 -90 -93 -95 -104 -107 -109 -115 -118 -126 -128 -131 -133 -136 -141 -144 -146 -155 -158 -170 -176 -180 -183 -186 -194 -201
0 0 23 20 13 4 2 -8 -13 -15 -26 -31 -36 -47 -54 -58 -63 -71 -75 -81 -86 -89 -91 -100 -103 -105 -111 -114 -122 -124 -127 -129 -132 -137 -140 -142 -151 -154 -166 -172 -176 -179 -182 -190 -197
0 0 0 28 21 12 10 0 -5 -7 -18 -23 -28 -39 -46 -50 -55 -63 -67 -73 -78 -81 -83 -92 -95 -97 -103 -106 -114 -116 -119 -121 -124 -129 -132 -134 -143 -146 -158 -164 -168 -171 -174 -182 -189
0 0 0 0 24 15 13 3 -2 -4 -15 -20 -25 -36 -43 -47 -52 -60 -64 -70 -75 -78 -80 -89 -92 -94 -100 -103 -111 -113 -116 -118 -121 -126 -129 -131 -140 -143 -155 -161 -165 -168 -171 -179 -186
0 0 0 0 0 22 20 10 5 3 -8 -13 -18 -29 -36 -40 -45 -53 -57 -63 -68 -71 -73 -82 -85 -87 -93 -96 -104 -106 -109 -111 -114 -119 -122 -124 -133 -136 -148 -154 -158 -161 -164 -172 -179
0 0 0 0 0 0 29 19 14 12 1 -4 -9 -20 -27 -31 -36 -44 -48 -54 -59 -62 -64 -73 -76 -78 -84 -87 -95 -97 -100 -102 -105 -110 -113 -115 -124 -127 -139 -145 -149 -152 -155 -163 -170
0 0 0 0 0 0 0 21 16 14 3 -2 -7 -18 -25 -29 -34 -42 -46 -52 -57 -60 -62 -71 -74 -76 -82 -85 -93 -95 -98 -100 -103 -108 -111 -113 -122 -125 -137 -143 -147 -150 -153 -161 -168
0 0 0 0 0 0 0 0 26 24 13 8 3 -8-15 -19 -24 -32 -36 -42 -47 -50 -52 -61 -64 -66 -72 -75 -83 -85 -88 -90 -93 -98 -101 -103 -112 -115 -127 -133 -137 -140 -143 -151 -158
0 0 0 0 0 0 0 0 0 29 18 13 8 -3-10 -14 -19 -27 -31 -37 -42 -45 -47 -56 -59 -61 -67 -70 -78 -80 -83 -85 -88 -93 -96 -98 -107 -110 -122 -128 -132 -135 -138 -146 -153
0 0 0 0 0 0 0 0 0 0 20 15 10 -1-8 -12 -17 -25 -29 -35 -40 -43 -45 -54 -57 -59 -65 -68 -76 -78 -81 -83 -86 -91 -94 -96 -105 -108 -120 -126 -130 -133 -136 -144 -151
0 0 0 0 0 0 0 0 0 0 0 26 21 103 -1 -6 -14 -18 -24 -29 -32 -34 -43 -46 -48 -54 -57 -65 -67 -70 -72 -75 -80 -83 -85 -94 -97 -109 -115 -119 -122 -125 -133 -140
0 0 0 0 0 0 0 0 0 0 0 0 26 158 4 -1 -9 -13 -19 -24 -27 -29 -38 -41 -43 -49 -52 -60 -62 -65 -67 -70 -75 -78 -80 -89 -92 -104 -110 -114 -117 -120 -128 -135
0 0 0 0 0 0 0 0 0 0 0 0 0 2013 9 4 -4 -8 -14 -19 -22 -24 -33 -36 -38 -44 -47 -55 -57 -60 -62 -65 -70 -73 -75 -84 -87 -99 -105 -109 -112 -115 -123 -130
0 0 0 0 0 0 0 0 0 0 0 0 0 024 20 15 7 3 -3 -8 -11 -13 -22 -25 -27 -33 -36 -44 -46 -49 -51 -54 -59 -62 -64 -73 -76 -88 -94 -98 -101 -104 -112 -119
0 0 0 0 0 0 0 0 0 0 0 0 0 0027 22 14 10 4 -1 -4 -6 -15 -18 -20 -26 -29 -37 -39 -42 -44 -47 -52 -55 -57 -66 -69 -81 -87 -91 -94 -97 -105 -112
0 0 0 0 0 0 0 0 0 0 0 0 0 000 26 18 14 8 3 0 -2 -11 -14 -16 -22 -25 -33 -35 -38 -40 -43 -48 -51 -53 -62 -65 -77 -83 -87 -90 -93 -101 -108
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 23 19 13 8 5 3 -6 -9 -11 -17 -20 -28 -30 -33 -35 -38 -43 -46 -48 -57 -60 -72 -78 -82 -85 -88 -96 -103
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 27 21 16 13 11 2 -1 -3 -9 -12 -20 -22 -25 -27 -30 -35 -38 -40 -49 -52 -64 -70 -74 -77 -80 -88 -95
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 25 20 17 15 6 3 1 -5 -8 -16 -18 -21 -23 -26 -31 -34 -36 -45 -48 -60 -66 -70 -73 -76 -84 -91
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 26 23 21 12 9 7 1 -2 -10 -12 -15 -17 -20 -25 -28 -30 -39 -42 -54 -60 -64 -67 -70 -78 -85
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 28 26 17 14 12 6 3 -5-7 -10 -12 -15 -20 -23 -25 -34 -37 -49 -55 -59 -62 -65 -73 -80
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 29 20 17 15 9 6 -2-4 -7 -9 -12 -17 -20 -22 -31 -34 -46 -52 -56 -59 -62 -70 -77
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 22 19 17 11 8 0-2 -5 -7 -10 -15 -18 -20 -29 -32 -44 -50 -54 -57 -60 -68 -75
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 28 26 20 17 974 2 -1 -6 -9 -11 -20 -23 -35 -41 -45 -48 -51 -59 -66
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 29 23 20 1210 7 5 2 -3 -6 -8 -17 -20 -32 -38 -42 -45 -48 -56 -63
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 25 22 1412 9 7 4 -1 -4 -6 -15 -18 -30 -36 -40 -43 -46 -54 -61
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 28 2018 15 13 10 5 2 0 -9 -12 -24 -30 -34 -37 -40 -48 -55
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 2321 18 16 13 8 5 3 -6 -9 -21 -27 -31 -34 -37 -45 -52
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 029 26 24 21 16 13 11 2 -1 -13 -19 -23 -26 -29 -37 -44
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 0028 26 23 18 15 13 4 1 -11 -17 -21 -24 -27 -35 -42
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 000 29 26 21 18 16 7 4 -8 -14 -18 -21 -24 -32 -39
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 000 0 28 23 20 18 9 6 -6 -12 -16 -19 -22 -30 -37
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 26 23 21 12 9 -3 -9 -13 -16 -19 -27 -34
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 28 26 17 14 2 -4 -8 -11 -14 -22 -29
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 29 20 17 5 -1 -5 -8 -11 -19 -26
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 22 19 7 1 -3 -6 -9 -17 -24
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 28 16 10 6 3 0 -8-15
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 19 13 9 6 3 -5-12
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 25 21 18 15 70
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 27 24 21 136
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 28 25 1710
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 28 2013
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 2316
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 024
enter:
0 1 2 3 4 2 2 3 4 4 2 3 3 233 3 2 3 3 3 3 3 3 3 3 3 3 333 3 3 3 3 3 3 3 3 3 3 3 3 34
0 1 2 3 4 5 3 4 4 4 5 4 4 554 4 5 5 5 4 4 4 5 5 5 5 4 455 5 5 5 5 5 4 5 5 5 4 5 5 55
0 0 2 3 4 5 6 4 4 5 6 4 5 665 5 6 6 6 5 5 6 6 6 6 6 5 666 6 6 6 6 6 6 6 6 6 6 6 6 66
0 0 0 3 4 5 6 7 5 5 6 7 7 677 7 6 7 7 7 7 7 7 7 7 7 7 777 7 7 7 7 7 7 7 7 7 7 7 7 77
0 0 0 0 4 5 6 7 5 6 6 7 7 677 7 6 7 7 7 7 7 7 7 7 7 7 777 7 7 7 7 7 7 7 7 7 7 7 7 77
0 0 0 0 0 5 6 7 8 9 7 7 8 778 8 7 7 8 8 8 8 7 8 8 8 8 878 8 8 8 8 8 8 8 8 8 8 8 8 88
0 0 0 0 0 0 6 7 8 9 10 8 9 1010 9 9 10 10 10 9 9 10 10 10 10 10 910 10 10 10 10 10 10 10 10 10 10 10 10 1010 10 10
0 0 0 0 0 0 0 7 8 9 10 9 9 1010 9 9 10 10 10 9 10 10 10 10 10 10 1010 10 10 10 10 10 10 10 10 10 10 10 10 1010 10 10
0 0 0 0 0 0 0 0 8 9 10 11 12 1011 11 12 10 11 11 11 12 12 11 11 11 11 1212 11 11 11 11 11 12 12 12 11 12 12 12 1212 12 12
0 0 0 0 0 0 0 0 0 9 10 11 12 1111 12 12 11 11 12 12 12 12 12 12 12 12 1212 12 12 12 12 12 12 12 12 12 12 12 12 1212 12 12
0 0 0 0 0 0 0 0 0 0 10 11 12 1112 12 12 12 12 12 12 12 12 12 12 12 12 1212 12 12 12 12 12 12 12 12 12 12 12 12 1212 12 12
0 0 0 0 0 0 0 0 0 0 0 11 12 1314 13 13 13 13 14 13 13 13 13 14 14 14 1313 13 14 14 14 14 14 14 14 14 14 14 14 1414 14 14
0 0 0 0 0 0 0 0 0 0 0 0 12 1314 15 13 14 14 14 15 15 14 14 14 14 15 1514 14 14 14 14 15 15 15 14 14 15 15 14 1414 15 15
0 0 0 0 0 0 0 0 0 0 0 0 0 1314 15 16 14 14 15 16 16 16 15 15 15 16 1616 15 15 15 15 16 16 16 16 15 16 16 16 1616 16 16
0 0 0 0 0 0 0 0 0 0 0 0 0 014 15 16 17 18 16 16 17 17 18 18 18 17 1718 18 18 18 18 17 17 17 18 18 17 18 18 1818 17 18
0 0 0 0 0 0 0 0 0 0 0 0 0 0015 16 17 18 19 17 17 17 18 19 19 19 17 1818 19 19 19 19 19 19 19 19 19 19 19 19 1919 19
0 0 0 0 0 0 0 0 0 0 0 0 0 000 16 17 18 19 20 21 18 19 19 19 20 20 1919 19 19 19 20 20 20 19 19 20 20 19 19 1920 20
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 17 18 19 20 21 22 19 19 20 20 21 2219 20 20 20 20 21 21 22 20 21 21 22 22 2221 22
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 18 19 20 21 22 23 20 21 22 22 2323 23 23 22 22 22 22 23 23 22 23 23 23 2322 23
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 19 20 21 22 23 24 25 22 22 2323 23 24 24 22 23 23 23 23 23 23 23 23 2323 24
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 20 21 22 23 24 25 26 23 2324 24 25 25 26 26 26 24 25 26 26 24 25 2526 26
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 21 22 23 24 25 26 27 2425 25 25 26 26 27 27 25 26 27 27 26 26 2627 27
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 22 23 24 25 26 27 2525 25 26 26 27 27 27 26 26 27 27 26 26 2627 27
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 23 24 25 26 27 2825 26 26 26 27 27 27 28 26 27 27 28 28 2827 28
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 24 25 26 27 2829 30 31 27 28 28 28 29 30 28 29 29 30 3028 30
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 25 26 27 2829 30 31 32 28 28 28 30 31 28 30 30 31 3128 31
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 26 27 2829 30 31 32 28 28 29 30 31 29 30 30 31 3130 31
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 27 2829 30 31 32 33 34 35 32 32 33 35 32 32 3233 35
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 2829 30 31 32 33 34 35 32 32 34 35 32 32 3234 35
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 029 30 31 32 33 34 35 36 33 35 36 36 36 3635 36
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 0030 31 32 33 34 35 36 37 35 36 36 36 37 3537
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 000 31 32 33 34 35 36 37 35 36 36 37 37 3637
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 000 0 32 33 34 35 36 37 36 36 36 37 37 3637
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 33 34 35 36 37 36 36 37 37 37 3637
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 34 35 36 37 38 37 37 37 37 3838
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 35 36 37 38 37 37 37 38 3838
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 36 37 38 39 37 38 38 3839
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 37 38 39 40 41 42 3942
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 38 39 40 41 42 3942
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 39 40 41 42 4344
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 40 41 42 4344
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 41 42 4344
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 42 4344
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 4344
0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 044
0,44: 4
0,4: 4
Consider the problem of neatly
5,44: 8
5,8: 8
printing a paragraph with
9,44: 12
9,12: 12
a monospaced font (all
13,44: 16
13,16: 16
characters having the same
17,44: 22
17,22: 22
width). The input text is a
23,44: 28
23,28: 28
sequence of n words of lengths
29,44: 36
29,36: 36
l 1, l 2, …, ln , measured
37,44: 42
37,42: 42
in characters, which are to be
43,44: 44
printed neatly
time costs: 3.478 ms
使用全部字符串,并只打印文本:(M=85)
Consider the problem of neatly printing a paragraph with a monospaced font (all
characters having the same width). The input text is a sequence of n words of lengths
l 1, l 2, …, ln , measured in characters, which are to be printed neatly on a
number of lines that hold a maximum of M characters each. No word exceeds the line
length, so that li ≤ M for i = 1, 2, …, n . The criterion of “neatness” is
as follows. If a given line contains words i through j , where i ≤ j , and exactly
one space appears between words, then the number of extra space characters at the end
of the line is , which must be nonnegative so that the words fit on the line. The
goal is 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. Analyze the running time and space
requirements of your algorithm.
time costs: 7.082 ms
可以观察到,第六行开头的 "as" 没有打印在第五行末尾,