Formatting Text - UVa 709 POJ 1093 dp

218 篇文章 0 订阅
206 篇文章 0 订阅

Formatting Text
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 2017 Accepted: 471

Description

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 n 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 is 1 + 7^2 = 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 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 (1 <= n <= 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 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 A and 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.

题意:使得每一行的字符数都为给定的字符数,并使每两个相邻单词之间的(空格-1)的平方的和最小。

思路:dp[i]表示从第i个开始新的一行的权值最小和。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
struct node
{ int len;
  char s[110];
}word[10010];
char c;
int len,num,dp[10010],sum[10010],p1,p2,num1,num2,sp;
void space()
{ c=cin.peek();
  while(c==' ')
  { cin.ignore();
    c=cin.peek();
  }
}
int solve(int i,int j)
{ if(i==j)
   return 500; //如果只有一个单词,这里直接返回500
  sp=len-(sum[j]-sum[i-1]);
  p1=sp/(j-i);p2=p1+1;
  num2=sp-p1*(j-i);
  num1=j-i-num2;
  return (p1-1)*(p1-1)*num1+(p2-1)*(p2-1)*num2;
}
void print(int l,int r)
{ int i,j,k;
  if(l==r)
  { printf("%s",word[l].s);
    printf("\n");
    return;
  }
  solve(l,r);
  printf("%s",word[l].s);
  for(i=1;i<=num1;i++)
  { for(j=1;j<=p1;j++)
     printf(" ");
    printf("%s",word[l+i].s);
  }
  for(i=1;i<=num2;i++)
  { for(j=1;j<=p2;j++)
     printf(" ");
    printf("%s",word[l+num1+i].s);
  }
  printf("\n");
}
int main()
{ int i,j,k,ret,pos;
  while(~scanf("%d",&len) && len)
  { space();
    cin.ignore();
    num=0;
    while(true)
    { space();
      if(c=='\n')
      { cin.ignore();
        break;
      }
      while(true)
      { space();
        if(c=='\n')
        { cin.ignore();
          break;
        }
        scanf("%s",word[++num].s);
        word[num].len=strlen(word[num].s);
      }
    }
    for(i=1;i<=num;i++)
     dp[i]=1000000000;
    dp[num+1]=0;
    for(i=1;i<=num;i++)
     sum[i]=sum[i-1]+word[i].len;
    for(i=num;i>=1;i--)
     for(j=i;j<=num;j++)
     { if(sum[j]-sum[i-1]+(j-i)>len)
        break;
       else
        dp[i]=min(dp[i],dp[j+1]+solve(i,j));
     }
    for(i=1;i<=num;)
    { pos=i;
      for(j=i;j<=num;j++)
      { if(sum[j]-sum[i-1]+(j-i)>len)
        break;
        if(dp[i]==dp[j+1]+solve(i,j))
         pos=j;
      }
      print(i,pos);
      i=pos+1;
    }
    printf("\n");
  }
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值