HDU 3722 Card Game 【KM】

Jimmy invents an interesting card game. There are N cards, each of which contains a string Si. Jimmy wants to stick them into several circles, and each card belongs to one circle exactly. When sticking two cards, Jimmy will get a score. The score of sticking two cards is the longest common prefix of the second card and the reverse of the first card. For example, if Jimmy sticks the card S1 containing "abcd" in front of the card S2 containing "dcab", the score is 2. And if Jimmy sticks S2 in front of S1, the score is 0. The card can also stick to itself to form a self-circle, whose score is 0.

For example, there are 3 cards, whose strings are S1="ab", S2="bcc", S3="ccb". There are 6 possible sticking:
1.  S1->S2, S2->S3, S3->S1, the score is 1+3+0 = 4
2.  S1->S2, S2->S1, S3->S3, the score is 1+0+0 = 1
3.  S1->S3, S3->S1, S2->S2, the score is 0+0+0 = 0
4.  S1->S3, S3->S2, S2->S1, the score is 0+3+0 = 3
5.  S1->S1, S2->S2, S3->S3, the score is 0+0+0 = 0
6.  S1->S1, S2->S3, S3->S2, the score is 0+3+3 = 6
So the best score is 6.

Given the information of all the cards, please help Jimmy find the best possible score.


Input There are several test cases. The first line of each test case contains an integer N (1 <= N <= 200). Each of the next N lines contains a string Si. You can assume the strings contain alphabets ('a'-'z', 'A'-'Z') only, and the length of every string is no more than 1000.

Output Output one line for each test case, indicating the corresponding answer. Sample Input
3
ab
bcc
ccb
1
abcd
Sample Output
6
0

题意:给定n个字符串,定义字符串两两连接的权值为:s1字符串翻转后匹配s2的最大公共前缀字符个数。自身也算是一个回环,求使得所有串形成一些回环的最大权值。

思路:KM变形题,建二分图时,将每个字符串编号为左右顶点集,边权值就是字符串i和j连接的权值,记得,i和j匹配时,翻转的是前者,并且自身和自身成环时权值始终为0。


#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
#define inf 0x3f3f3f3f
const int maxn = 210;
int f[maxn],vis[maxn][maxn],nx[maxn],ny[maxn],len[maxn];
int visx[maxn],visy[maxn];
char str[maxn][maxn*5];
int n,d;
int Judge(char *s,int l1,char *s2,int l2)
{
	int sum=0,i=l1-1,j=0;
	while(i>=0&&j<l2)
	{
		if(s[i] == s2[j])
			sum ++;
		else
			return sum;//如果不匹配,直接返回 
		i--;
		j++;
	}
	return sum;
}
int dfs(int x)
{
	int y,dis;
	visx[x] = 1;
	for(y = 1; y <= n;y ++)
	{
		if(!visy[y])
		{
			dis = nx[x]+ny[y]-vis[x][y];
			if(dis == 0)
			{
				visy[y] = 1;
				if(f[y] == -1||dfs(f[y]))
				{
					f[y] = x;
					return 1;
				}
			}
			else if(dis < d)//找到满足可修改可行定标的最小常数 
				d = dis;
		}
	}
	return 0;
}
int KM()
{
	int x,y,sum,i;
	memset(f,-1,sizeof(f));//存储当前顶点的增广路经 
	memset(nx,0,sizeof(nx));
	memset(ny,0,sizeof(ny));//貌似是因为初始化的锅? 
	for(x = 1; x <= n; x ++)
		for(y = 1,nx[x]=-inf; y <= n; y ++)
		{
			if(nx[x]  < vis[x][y])
				nx[x] = vis[x][y];//初始化为x点集中权值最大的点
				 
		}
	for(x = 1; x <= n; x ++)
	{
		while(1)
		{
			d = inf;
			memset(visx,0,sizeof(visx));
			memset(visy,0,sizeof(visy));
			if(dfs(x))//如果找到了增广路,直接退出 
				break;
			for(i = 1; i <= n; i ++)//继续查找增广路 
				if(visx[i])
					nx[i] -= d;
			for(i = 1; i <= n; i ++)
				if(visy[i])
					ny[i] += d;
		}
	}
	sum = 0;
	for(i = 1; i <= n; i ++)	
		if(f[i]!=-1)
			sum += vis[f[i]][i];
	return sum;	
}
int main()
{
	int i,j;
	while(scanf("%d",&n)!=EOF)
	{
		memset(vis,0,sizeof(vis));
		for(i = 1; i <= n; i ++)
		{
			scanf("%s",str[i]);
			len[i] = strlen(str[i]);
		}
		for(i = 1; i <= n; i ++)
			for(j = 1; j <= n; j ++)
			{
				if(i == j)
					continue; 
				vis[i][j] = Judge(str[i],len[i],str[j],len[j]);//建带权值的二分图 
			} 
		
		printf("%d\n",KM());	
		
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值