HDU3722 Card Game 二分图之最优匹配 KM算法

Problem Description

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="abcd",s2="dbca",将s1拼接在s2的后面,所得到的值就是将s1翻转得到s1'="dcba",s1'和s2共有的前缀为1;其值就是1;现在要求的就是讲这些拼起来得到的值为最大。将字符串分成X集合和Y集合,每个集合均有n个字符串,任意两个字符串得到的值作为其权值,最后利用简单的KM就能实现

 

代码:

#include<iostream>
using namespace std;
#define MAX 201
#define MAXN 0xffffff
int map[MAX][MAX],link[MAX],dx[MAX],dy[MAX];
bool visx[MAX],visy[MAX];
int n,lack;
char A[201][10001];
bool DFS(int v)
{
 visx[v]=true;
 for(int i=1;i<=n;i++)
 {
  if(visy[i])
   continue;
  int t=dx[v]+dy[i]-map[v][i];
  if(!t)
  {
   visy[i]=true;
   if(link[i]==-1||DFS(link[i]))
   {
    link[i]=v;
    return true;
   }
  }
  else
  {
   if(t<lack)
    lack=t;
  }
 }
 return false;
}
void KM()
{
 int i,j;
 memset(dx,0,sizeof(dx));
 memset(dy,0,sizeof(dy));
 memset(link,-1,sizeof(link));
 for(i=1;i<=n;i++)
 {
  for(j=1;j<=n;j++)
   if(map[i][j]>dx[i])
    dx[i]=map[i][j];
 }
 for(i=1;i<=n;i++)
 {
  while(true)
  {
   memset(visx,0,sizeof(visx));
   memset(visy,0,sizeof(visy));
   lack=MAXN;
   if(DFS(i))
    break;
   for(j=1;j<=n;j++)
   {
    if(visx[j])
     dx[j]-=lack;
    if(visy[j])
     dy[j]+=lack;
   }
   
  }
 }
 int ans=0;
 for(i=1;i<=n;i++)
  ans+=map[link[i]][i];
    cout<<ans<<endl;
}
void Q()
{
 int i,j,p,q;
 for(i=1;i<=n;i++)
 {
  int len1=strlen(A[i]);
  for(j=1;j<=n;j++)
  {
   if(i==j)
    map[i][j]=0;
   else
   {
    int len2=strlen(A[j]);
    q=0;
    p=len2-1;
    while(q<len1&&p>=0&&A[i][q]==A[j][p])
    {
     q++;p--;
    }
    map[i][j]=q;
   }
  }
 }
}
int main()
{
 while(cin>>n)
 {
  int i,j;
  for(i=1;i<=n;i++)
  {
   for(j=1;j<=n;j++)
    map[i][j]=0;
  }
  for(i=1;i<=n;i++)
   scanf("%s",A[i]);
  Q();
  KM();
 }
 return 0;
}

咱能力有限,代码写的不漂亮,如有瑕疵,请评论指教!!!!!!!!!!!!!

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值