poj 2817 状态DP

本文介绍了一种名为WordStack的文字游戏算法实现,该算法旨在通过排列一系列单词并填充空格来最大化相同字符的数量,以此获得尽可能高的分数。文章详细解释了输入输出格式,并提供了一个具体的样例输入输出以帮助理解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

WordStack
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3248 Accepted: 1171
Description


As editor of a small-town newspaper, you know that a substantial number of your readers enjoy the daily word games that you publish, but that some are getting tired of the conventional crossword puzzles and word jumbles that you have been buying for years. You decide to try your hand at devising a new puzzle of your own. 


Given a collection of N words, find an arrangement of the words that divides them among N lines, padding them with leading spaces to maximize the number of non-space characters that are the same as the character immediately above them on the preceding line. Your score for this game is that number.
Input


Input data will consist of one or more test sets. 


The first line of each set will be an integer N (1 <= N <= 10) giving the number of words in the test case. The following N lines will contain the words, one word per line. Each word will be made up of the characters 'a' to 'z' and will be between 1 and 10 characters long (inclusive). 


End of input will be indicated by a non-positive value for N .
Output


Your program should output a single line containing the maximum possible score for this test case, printed with no leading or trailing spaces.
Sample Input



abc 
bcd 
cde 
aaa 
bfcde 
0
Sample Output


8
Hint


Note: One possible arrangement yielding this score is: 
aaa 


abc 


 bcd


  cde 


bfcde


#include <stdio.h>
#include <string.h>
#include <string>
using namespace std;

int dp[1<<(12)][12];
int max(int a, int b)
{
	if(a > b)
		return a;
	return b;
}
int main()
{
	char str[12][12];
	int n;
	int len[12][12]; //len[i][j]表示第i个字符串和第j个字符串的最大公共字符长度

	while((scanf("%d", &n) != EOF) && (n > 0))
	{
		memset(len, 0, sizeof(len));
		int i, j, k, l, count, maxx;

		for(i = 0; i < n; i++)
		{
			scanf("%s", str[i]);
		}

		for(i = 0; i < n; i++)
		{
			for(j = 0; j < n; j++) 
			{
				if(i != j)
				{
					int len1 = strlen(str[i]);
					int len2 = strlen(str[j]);
					maxx = -1;

					for(k = 0; k < len1; k++)
					{
						count = 0;
						for(l = 0; (l < len2) && (l+k < len1); l++)//for(l=0;(l<len1 && l+k < len1); l++) if(str[i][l] == str[j][l+k]) count++
						{
							if(str[i][l+k] == str[j][l])
								count++;
						}

						if(count > maxx)
							maxx = count;
					}

					if(maxx > len[i][j])
						len[i][j] = len[j][i] = maxx;
				}
			}
		}

		memset(dp, 0, sizeof(dp));

		for(i = 0; i < (1<<n); i++)
		{
			for(j = 0; j < n; j++)
			{
				if(i&(1<<j))
				{
					for(k = 0; k < n; k++)
					{
						if(!(i&(1<<k)))
						{
							dp[i|(1<<k)][k] = max(dp[i|(1<<k)][k], dp[i][j]+len[j][k]);
						}
					}
				}
			}
		}

		maxx = -1;
		for(i = 0; i < (1<<n); i++)
		{
			for(j = 0; j < n; j++)
			{
				if(dp[i][j] > maxx)
					maxx = dp[i][j];
			}
		}

		printf("%d\n", maxx);
	}
	return 0;
}
	


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值