UVA 709 Formatting Text(记忆化搜索)

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 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 + 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 A and B be two solutions. Find the first gap which has not the same length in A andB. 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.

题意:给一个n,表示一行要放n个字母,然后给你一篇文章,可以在文章任意单词之间加入空格,来使得文章符合规范,然后要计算bad值,计算方法为:如果单独一个单词为一行+500,两个词之间的空格数c,+(c - 1) ^ 2,求出使得bad最小时,文章的排版。

思路:记忆化搜索。一个个单词放过去,考虑单独放一行,和之间隔k个空格的情况。dp[i][j]表示第i个单词放在当行第j个位置时候的bad最小状态。用一个rec数组记录状态转移过程用于输出结果。

代码:

#include <stdio.h>
#include <string.h>
const int INF = 1 << 30;
const int MAXN = 10005;
const int N = 105;
int min(int a, int b) {return a < b ? a : b;}
int i, j, n, len[MAXN], num, dp[MAXN][N], rec[MAXN][N];
char str[MAXN], word[MAXN][N];

void init() {
	num = 0;
	memset(dp, -1, sizeof(dp));
	memset(rec, -1, sizeof(rec));
	while (gets(str) && str[0] != '\0') {
		int lens = strlen(str);
		for (int i = 0; str[i];) {
			sscanf(str + i, "%s", word[num]);
			len[num] = strlen(word[num]);
			i += len[num ++];
			while(str[i] == ' ') i ++;
		}
	}
}

int DP(int i, int j) {
	if (dp[i][j] != -1)
		return dp[i][j];
	dp[i][j] = INF;
	if (i == num) {
		if (j == 0) {
			return dp[i][j] = 0;
		}
		else {
			return dp[i][j];
		}
	}
	if (j == 0) {
		int ans = DP(i + 1, 0);
		if (dp[i][j] > 500 + ans) {
			dp[i][j] = 500 + ans;
			rec[i][j] = 0;
		}
	}
	if (j + len[i] == n) {
		int ans = DP(i + 1, 0);
		if (dp[i][j] > ans) {
			dp[i][j] = ans;
			rec[i][j] = 0;
		}
		return dp[i][j];
	}
	else {
		int x = j + len[i] + 1;
		for (int k = x; k < n; k ++) {
			if (k + len[i + 1] > n)
				break;
			int t = k - x;
			int ans = DP(i + 1, k);
			if (dp[i][j] > t * t  + ans) {
				dp[i][j] = t * t  + ans;
				rec[i][j] = k;
			}
		}
	}
	return dp[i][j];
}

void print(int i, int j) {
	if (rec[i][j] == -1)
		return;
	printf("%s", word[i]);
	if (rec[i][j] == 0)
		printf("\n");
	else {
		for (int k = 0; k < rec[i][j] - j - len[i]; k ++)
			printf(" ");
	}
	print(i + 1, rec[i][j]);
}

int main() {
	while (~scanf("%d%*c", &n) && n) {
		init();
		DP(0, 0);
		print(0, 0);
		printf("\n");
	}
	return 0;
}


你可能感兴趣的:(UVA 709 Formatting Text(记忆化搜索))