HDU 3722 Card Game 二分匹配

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. 

InputThere 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. 

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

题意:给出n个字符串,每次从这n个字符串中挑选两个字符串,让第二个字符串反转,判断反转后的第二个字符串前缀和第一个字符串前缀相同部分的长度n,这两个字符串组合起来对应的权值即为n,例如样例第一个:第二个字符串为bcc,第三个为cbb,将第三个字符串反转之后为bbc,和第二个字符串前缀相同的长度为3,所以S2->S3的权值为3,找到最大的匹配方案,使匹配后的权值和最大。


思路:km算法,关键在于构图,双循环字符串比较构建图,然后km模板带入就可以了。

#include<stdio.h>
#include<string.h>
int a[1100][1100],x1[1100],x2[1100],y1[1100],y2[1100],c[1100],match[1100],s1[1100];
char str[1100][1100];
int m,n;
int max(int x,int y)
{
	if(x<y)
		x=y;
	return x;
}

int min(int x,int y)
{
	if(x>y)
		x=y;
	return x;
}

int dfs(int w)
{
	
	int i,t;
	x2[w]=1;
	for(i=1;i<=n;i++)
	{
		if(y2[i])
			continue;
		t=x1[w]+y1[i]-a[w][i];
		if(t==0)
		{
			y2[i]=1;
			if(match[i]==-1||dfs(match[i]))
			{
				match[i]=w;
				return 1;
			}	
		}
		else
		{
			c[i]=min(c[i],t);
		}
	}	
	return 0;
}

void km()
{
	int i,j,t;
	memset(match,-1,sizeof(match));
	memset(y1,0,sizeof(y1));
	for(i=1;i<=n;i++)
	{
		x1[i]=a[i][1];
		for(j=2;j<=n;j++)
		{
			x1[i]=max(x1[i],a[i][j]);
		}
	}
	
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=n;j++)
			c[j]=1e9;
			
		while(1)
		{
			memset(x2,0,sizeof(x2));
			memset(y2,0,sizeof(y2));
			if(dfs(i))
				break;
			t=1e9;
			for(j=1;j<=n;j++)
			{
				if(!y2[j]&&t>c[j])
					t=c[j];
			}
			for(j=1;j<=n;j++)
			{
				if(x2[j])
					x1[j]-=t;
			}
			for(j=1;j<=n;j++)
			{
				if(y2[j])
					y1[j]+=t;
				else
					c[j]-=t;
			}
		}
	}
	int sum=0;
	for(i=1;i<=n;i++)
	{
		if(match[i]==-1||a[match[i]][i]==0)
        {
            continue;
        }
		sum+=a[match[i]][i];
	}
	printf("%d\n",sum);
}
int add(char s1[],int l1,char s2[])//构图函数
{
	int i,j,k=0;
	
	for(i=l1-1;i>=0;i--)
	{
		if(s1[i]==s2[k])
		{
			k++;
		}
		else
		{
			break;
		}
	}
	return k;
}
int main()
{
	int i,j,k,sum,t;
	while(scanf("%d",&n)!=EOF)
	{
		for(i=1;i<=n;i++)
		{
			scanf("%s",str[i]);
		}
		for(i=1;i<=n;i++)//双循环构图
		{
			for(j=1;j<=n;j++)
			{
				if(i!=j)
				{
					sum=0;
					int l1=strlen(str[i]);
					int l2=strlen(str[j]);
					sum=add(str[i],l1,str[j]);	
					a[i][j]=sum;
				}
				else
				{
					a[i][j]=0;
				}	
					
			}
		}
		km();
	}
	return 0;
}


猜你喜欢

转载自blog.csdn.net/seven_deadly_sins/article/details/80395605