p1093_Formatting Text_(worth thinking)
来源:互联网 发布:php权威指南百度云 编辑:程序博客网 时间:2024/06/06 20:00
Formatting Text
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 717 Accepted: 147
Description
Writingse-mails is fun, but, unfortunately, they do not look very nice, mainlybecause not all lines have the same lengths. In this problem, your taskis to write an e-mail formatting program which reformats a paragraph ofan e-mail (e.g. by inserting spaces) so that, afterwards, all lineshave the same length (even the last one of each paragraph).
The easiest way to perform this task would be to insert morespaces between the words in lines which are too short. But this is notthe best way. Consider the following example:
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.) Theonly exception to this is the following:
If a line contains only one word this word shall be put at thebeginning of the line, and a badness of 500 is assigned to this line ifit is shorter than it should be. (Of course, in this case, the lengthof the line is simply the length of the word.)
The easiest way to perform this task would be to insert morespaces between the words in lines which are too short. But this is notthe best way. Consider the following example:
****************************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.
****************************Butthis 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 abetter result:
This is the example you are
actually considering.
****************************Ofcourse, this has to be formalized. To do this, we assign a badness toeach 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 allbadnesses. 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.
This is the example you
are actually considering.
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.) Theonly exception to this is the following:
If a line contains only one word this word shall be put at thebeginning of the line, and a badness of 500 is assigned to this line ifit is shorter than it should be. (Of course, in this case, the lengthof the line is simply the length of the word.)
Input
Theinput contains a text consisting of several paragraphs. Each paragraphis preceded by a line containing a single integer n, the desired widthof the paragraph (1 <= n <= 80).
Paragraphs consist of one or more lines which contain one or morewords each. Words consist of characters with ASCII codes between 33 and126, inclusive, and are separated by spaces (possibly more than one).No word will be longer than the desired width of the paragraph. Thetotal length of all words of one paragraph will not be more than 10000characters.
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.
Paragraphs consist of one or more lines which contain one or morewords each. Words consist of characters with ASCII codes between 33 and126, inclusive, and are separated by spaces (possibly more than one).No word will be longer than the desired width of the paragraph. Thetotal length of all words of one paragraph will not be more than 10000characters.
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 samebadness, use the following algorithm to choose which one to output: LetA and B be two solutions. Find the first gap which has not the samelength in A and B. Do not output the solution in which this gap isbigger.
Output a blank line after each paragraph.
If there are several ways to format a paragraph with the samebadness, use the following algorithm to choose which one to output: LetA and B be two solutions. Find the first gap which has not the samelength in A and B. Do not output the solution in which this gap isbigger.
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.
Source
Mid-Central European Regional Contest 1999
题目大意:
在graph的单词之间添加空格,使得每一行的长度是给出的n,两单词间k个空格的不满意度是(k-1)^2,求一种方法使得空格造成的不满意度最小,并且行首行尾均为单词(除非一行只有一个单词,如果此单词长度<n 不满意度数为500)。
构造的最优解是使得前面的空格个数尽量的小。
分析:
1.无论分成多少行,每个单词前面的空格个数和行数无关。
2.每行必有一个单词开头和结尾(特殊情况特殊讨论);
那么根据这两个条件就可以知道:
以某个单词为某一行结尾的最优的解(设mn[i])一定和以他前面的某个单词结尾的最优值有关系;并且他们之间的单词和这个i单词在一行;
这样就很容易知道以最后一个单词为一行结尾的最优值是多少了~;
3.上面的问题并没有完全解决,如果i----j单词是一行的话那么最优解是多少呢。。
这个也简单,预处理一下就好了,就是i----j单词是一行要用规定的n个格子,那么枚举j单词前面的空格数k,就是要知道i----j-1单词用了n-len[j]-k个格子的最优值就可以了~;
但是!有大牛想到了一个特性:平方和最小,一定是大家都很平均了!那么就是尽量的平分。。任意两个空格长度相差不到2的时候当然一定最优。
现在最优解的求解已经完成了。
看如何构造特定的一组解。
4.规定前面的空格数尽量的小====>前面的单词尽量的紧密-===>前面的单词占的行数尽量的少同时后面的行单词尽量的少===>可以记录当前最优值能使得i所在的行号,另外枚举上一行最后一个单词的时候应该从后向前枚举,保证的是i在当前行的所有情况下当前行的单词尽量的少,也就是保证了前面的单词尽量的多~
这样就可以求出最优解了~
后记:
1.最开始看的时候就想到了类似方差的问题,在隔了一天之后想到mn[M]的方法的时候竟然毫无感觉的想去预处理枚举。。。思维很。。。。
2.在没有提示的条件下首先想着测试数据,被老大发现之后进行说服教育...感触很深,对么。。。怎么可以这么懒呢。。思想懒惰很可怕。。。
3.自己想的时候想到了mn[M][R]的方法表示的当前单词占据在某一行的第j个位置时候的最优值,可惜啊。。。没有想到需要行数尽量的小。。。显然没有感觉到问题的本质。。。想法很浮躁,想“懂”了就写。。。服了。。
4.和老大讨论过之后就觉得那是对的,根本没有深想。。pe了多少次。。。。。还以为是细节出问题了。。。。再这么懒就会变成sz的!
- p1093_Formatting Text_(worth thinking)
- thinking
- Thinking
- thinking
- thinking
- thinking
- Thinking
- THINKING
- thinking
- thinking
- thinking
- Thinking
- thinking
- Thinking
- It's worth it!
- what is worth doing is worth doing well
- Is College Education Worth it?
- How Much Is My Blog Worth?
- 继承---派生类(C++)
- 不应该忽视的 AIX 命令
- 关于PHP服务器变量$_SERVER详解
- VB.Net程序代码:Hashtable与ListView结合使用测试例子
- 开通
- p1093_Formatting Text_(worth thinking)
- 试验wii上UL的初步结论
- 笑破你肚皮的夜间护花行动
- 电脑运行慢怎么办
- 搞爆编译器的代码
- Intersection
- API实用函数大集合
- 盖茨语录(中英双文,很好的东西)
- [校招]2010IBM中国系统与科技开发中心(CSTL)招聘职位