# Uva--709（动规，字符串处理）

2014-08-23 16:16:01

Writings e-mails is fun, but, unfortunately, they do not look very nice, mainly because not all lines have the same lengths. In this problem, your task is to write an e-mail formatting program which reformats a paragraph of an e-mail (e.g. by inserting spaces) so that, afterwards, all lines have the same length (even the last one of each paragraph).

The easiest way to perform this task would be to insert more spaces between the words in lines which are too short. But this is not the best way. Consider the following example:

****************************
This is the example you are
actually considering.


Let us assume that we want to get lines as long as the row of stars. Then, by simply inserting spaces, we would get

****************************
This is the example you  are
actually        considering.


But this looks rather odd because of the big gap in the second line. By moving the word are'' from the first to the second line, we get a better result:

****************************
This  is  the  example   you
are  actually   considering.


Of course, this has to be formalized. To do this, we assign a badness to each gap between words. The badness assigned to a gap of nspaces is (n - 1)2. The goal of the program is to minimize the sum of all badnesses. For example, the badness of the first example is 1 + 72 = 50 whereas the badness of the second one is only 1 + 1 + 1 + 4 + 1 + 4 = 12.

In the output, every line has to start and to end with a word. (I.e. there cannot be a gap at the beginning or the end of a line.) The only exception to this is the following:

If a line contains only one word this word shall be put at the beginning of the line, and a badness of 500 is assigned to this line if it is shorter than it should be. (Of course, in this case, the length of the line is simply the length of the word.)

## Input

The input file contains a text consisting of several paragraphs. Each paragraph is preceded by a line containing a single integer n, the desired width of the paragraph ( ).

Paragraphs consist of one or more lines which contain one or more words each. Words consist of characters with ASCII codes between 33 and 126, inclusive, and are separated by spaces (possibly more than one). No word will be longer than the desired width of the paragraph. The total length of all words of one paragraph will not be more than 10000 characters.

Each paragraph is terminated by exactly one blank line. There is no limit on the number of paragraphs in the input file.

The input file will be terminated by a paragraph description starting with n=0. This paragraph should not be processed.

## Output

Output the same text, formatted in the way described above (processing each paragraph separately).

If there are several ways to format a paragraph with the same badness, use the following algorithm to choose which one to output: Let Aand B be two solutions. Find the first gap which has not the same length in A and B. Do not output the solution in which this gap is bigger.

Output a blank line after each paragraph.

## Sample Input

28
This is the example you are
actually considering.

25
Writing e-mails is fun, and with this program,
they even look nice.

0

## Sample Output

This  is  the  example   you
are  actually   considering.

Writing e-mails  is  fun,
and  with  this  program,
they  even   look   nice.话外：虽然不知道office里面是不是这样处理的，但这题让我看到了算法在实际应用中的影子，很棒。思路：这题让我从上午写到了下午，QAQ，纯自己YY的。虽然效率不是特高（写的有点挫，哈）。怎么开始分析呢，首先找这个问题的子问题，假设最终答案是构成 L 行，那么如果我们计算出 前 i 个（1 <= i <= 总数N）单词构成 L - 1 行的最小badness，然后再枚举第 L 行放多少个单词，就可以这样转移状态：设第 L 行放 k 个，那么 前 N - k 个单词构成前 （L - 1） 行的最小 badness 加上 k 个单词放在第 L 行的 badness 就是整个问题（前 N 个单词构成 L 行的最小 badness ）的解了。那么就可以定义 dp 数组了，dp[i][j] 表示用前 i 个单词构成前 j 行的最小 badness，转移方程：dp[i][j] = min(dp[i - k][j - 1]) (1 << k << i - j + 1)，枚举了第 j 行放 k 个单词。在DP过程中还要记录路径，我开了pre数组。这题还有个细节，那就是 k 个单词放某行的最小 badness，这个很好想，见程序。（个人觉得这个程序写烦了，还有可能是数据水把，数组没开到边界就A了QAQ）
  1 #include <cstdio>
2 #include <cstring>
3 #include <cmath>
4 #include <string>
5 #include <sstream>
6 #include <iostream>
7 using namespace std;
8 const int INF = 1 << 30;
9
10 struct word{
11     char s[85];
12 }w[1005];
13
14 int n;
15 int cnt;
16 int dp[1005][1005];
17 int pre[1005][1005][5]; // 0 : 个数 , 1 : High , 2 : Low , 3 : A , 4 : B.
18 int sum[1005];
19 int Re[5]; // 1 : High , 2 : Low , 3 : A , 4 : B.
20
21 int Cal(int l,int r){ // [l,r] 取数
22     memset(Re,0,sizeof(Re));
23     int gaplen = n - (sum[r] - sum[l - 1]);
24     int gapnum = r - l;
25     if(gapnum == 0){
26         if(gaplen > 0)
27             return 500;
28         else if(gaplen == 0)
29             return 0;
30     }
31     int high = ceil(1.0 * gaplen / gapnum);
32     int low = gaplen / gapnum;
33     Re[1] = high,Re[2] = low;
34     if(high == low){
35         Re[4] = gapnum;
36         return gapnum * (high - 1) * (high - 1);
37     }
38     else{
39         int a = gaplen - gapnum * low;
40         int b = gapnum - a;
41         Re[3] = a,Re[4] = b;
42         return a * low * low + b * (low - 1) * (low - 1);
43     }
44 }
45
46 void Print(int x,int y){
47     if(x == 0 || !pre[x][y][0])
48         return;
49     int k[5];
50     for(int i = 0; i < 5; ++i) k[i] = pre[x][y][i];
51     Print(x - k[0],y - 1);
52     printf("%s",w[x - k[0] + 1].s);
53     for(int i = x - k[0] + 2; i <= x; ++i){
54         if(k[4]){
55             for(int j = 0; j < k[2]; ++j) printf(" ");
56             --k[4];
57         }
58         else    for(int j = 0; j < k[1]; ++j) printf(" ");
59         printf("%s",w[i].s);
60     }
61     puts("");
62 }
63
64 int main(){
65     char str[200];
66     while(scanf("%d",&n) == 1 && n){
67         getchar();
68         memset(w,0,sizeof(w));
69         memset(pre,0,sizeof(pre));
70         cnt = 1;
71         string str;
72         while(getline(cin,str)){
73             int len = str.size();
74             if(len == 0) break;
75             else{
76                 istringstream sin(str);
77                 while(sin >> w[cnt].s){
78                     sum[cnt] = (cnt == 1 ? strlen(w[cnt].s) : sum[cnt - 1] + strlen(w[cnt].s));
79                     ++cnt;
80                 }
81             }
82         }
83         --cnt;
84         for(int i = 0; i <= cnt; ++i)
85             for(int j = 0; j <= cnt; ++j)
86                 dp[i][j] = INF;
87         dp[0][0] = 0;
88         for(int j = 1; j <= cnt; ++j){
89             for(int i = j; i <= cnt; ++i){
90                 if(sum[i] + i - j> n * j)
91                     continue;
92                 for(int k = 1; k <= i - j + 1; ++k){ // k 表示参与这一行的单词数
93                     if(sum[i] - sum[i - k] + k - 1> n)
94                         break;
95                     int tmp;
96                     if(dp[i - k][j - 1] != INF && ((tmp = dp[i - k][j - 1] + Cal(i - k + 1,i)) < dp[i][j])){
97                         dp[i][j] = tmp;
98                         pre[i][j][0] = k;
99                         for(int m = 1; m < 5; ++m) pre[i][j][m] = Re[m];
100                     }
101                 }
102             }
103         }
104         int pos,tmin = INF;
105         for(int i = 1; i <= cnt; ++i){
106             if(dp[cnt][i] < tmin){
107                 tmin = dp[cnt][i];
108                 pos = i;
109             }
110         }
111         Print(cnt,pos);
112         printf("\n");
113     }
114     return 0;
115 }


posted @ 2014-08-23 16:28  Naturain  阅读(137)  评论(0编辑  收藏  举报