uva709 - Formatting Text

来源:互联网 发布:淘宝stussy潮牌汇 编辑:程序博客网 时间:2024/06/05 02:04

Formatting Text 

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 samelength (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 bestway. Consider the following example:

****************************This is the example you areactually considering.

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

****************************This is the example you  areactually        considering.

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

****************************This  is  the  example   youare  actually   considering.

Of course, this has to be formalized. To do this, we assign abadness to each gap between words.The badness assigned to a gap ofn spaces 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 is1 + 72 = 50whereas 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 beginningof 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 simplythe length of the word.)

Input 

The input file contains a text consisting of severalparagraphs. Each paragraph is preceded by a line containing a single integern, the desired width of the paragraph ($1 \le n \le 80$).

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 onthe 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: LetA andB betwo solutions. Find the first gap which has not the same length inA andB.Do not output the solution in which this gap is bigger.

Output a blank line after each paragraph.

Sample Input 

28This is the example you areactually considering.25Writing e-mails is fun, and with this program,they even look nice.0

Sample Output 

This  is  the  example   youare  actually   considering.Writing e-mails  is  fun,and  with  this  program,they  even   look   nice.

 

  每行的字符数不能超过n,一个坏的值为单词间空格数-1的平方,末尾不能有空格,如果一行只有一个单词,那么这一行坏的值是500,问最小的坏的值是多少。

  我想的是先算出到每行结束最多到第几个单词,然后用二维数组dp[i][j]表示第i行以第j个单词结束的最小坏值,递推的同时记下路径。结果做完后超时。。要开10000*10000的数组。。这样思路就有问题。。结果别人是用dp[i][j]表示第i个单词的开头在行中的第j个位置,从这个单词到最后一个单词的最小坏值,这样就只用10000*80左右的数组了。有个地方要注意的是,最后一个单词不是放在最后就是单独一行,因此我们可以设最后一个单词后面还有一个长度为0的单词,如果放在单独一行就返回0,否则返回INF。

  记忆化搜索做,顺着的,打印的时候也是照这个顺序。

  还有就是给的单词间不只一个空格。。被坑了好久。。

#include<cstring>#include<cstdio>#include<iostream>#include<climits>#include<cmath>#include<algorithm>#include<queue>#define INF 0x3f3f3f3fusing namespace std;int l[10010],d[10010][90];char str[10010],a[10010][90];int N,M;int DP(int x,int y){    int &ans=d[x][y];    if(ans!=-1) return ans;    ans=INF;    if(x==M){        if(y==1) return ans=0;        return ans;    }    if(y==1) ans=min(ans,DP(x+1,1)+500);    if(y+l[x]-1==N) return ans=min(ans,DP(x+1,1));    int i,m,n=y+l[x]+1;    for(i=n;i<=N;i++){        if(i+l[x+1]-1>N) break;        m=i-n;        ans=min(ans,DP(x+1,i)+m*m);    }    return ans;}void print(int x,int y){    if(x>=M) return;    printf("%s",a[x]);    if(y==1&&d[x][y]==d[x+1][1]+500){        puts("");        print(x+1,1);    }    else if(y+l[x]-1==N){        puts("");        print(x+1,1);    }    else{        int i,j,m,n=y+l[x]+1;        for(i=n;i<=N;i++){            if(i+l[x+1]-1>N) break;            m=i-n;            if(d[x][y]==d[x+1][i]+m*m){                for(j=0;j<=m;j++) printf(" ");                print(x+1,i);                break;            }        }    }}int main(){    freopen("in.txt","r",stdin);    while(scanf("%d",&N),N){        getchar();        int i,j,k;        M=1;        memset(l,0,sizeof(l));        memset(d,-1,sizeof(d));        while(gets(str),str[0]){                int p=0,L;            while(sscanf(str+p,"%s",a[M]),p<strlen(str)){                l[M]=strlen(a[M]);                p+=l[M];                while(str[p]==' ') p++;                M++;            }        }        DP(1,1);        print(1,1);        puts("");    }    return 0;}