p1093_Formatting Text_(worth thinking)

Formatting Text
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 717   Accepted: 147

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.

Source


題目大意:
在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的!
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章