hdu 3722 Card Game 求完全图中l个不相交的环的最大权和=最优匹配 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

 

//

 

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
const int N=210;
const int inf=(1<<28);
//w是完全图,若i->j没有路,则w[i][j]=inf;
//若有多条路,则取最小路
int n,m;//点数1->n,边数
int match[N],lack,w[N][N],lx[N],ly[N];;
bool visx[N],visy[N];
bool dfs(int x){
    visx[x]=true;
    for(int y=1; y<=n; y++){
        if(visy[y]) continue;
        int t=lx[x]+ly[y]-w[x][y];
        if(t==0){
            visy[y]=true;
            if(match[y]==-1||dfs(match[y])){
                match[y]=x;
                return true;
            }
        }
        else if(lack>t) lack=t;
    }
    return false;
}
int KM(){
 int i,j;
 for(i=0;i<=n;i++)lx[i]=0;
 for(i=0;i<=n;i++)ly[i]=0;
    memset(match,-1,sizeof(match));
    for(i=1; i<=n; i++)for( j=1; j<=n; j++)if(w[i][j]>lx[i]) lx[i]=w[i][j];
    for(int x=1; x<=n; x++){
        while(1){
            memset(visx,0,sizeof(visx));
            memset(visy,0,sizeof(visy));
            lack=inf;
            if(dfs(x)) break;
            for(i=1; i<=n; i++){
                if(visx[i]) lx[i]-=lack;
                if(visy[i]) ly[i]+=lack;
            }
        }
    }
    int total=0;
    for(i=1;i<=n;i++)
    {
        total+=w[match[i]][i];
    }
    return total;
}
char s[1010][1010];
int len[1010];
int main()
{
    int i,j;
    //freopen("c.txt","r",stdin);
    while(scanf("%d",&n)!=EOF)
    {
        for(i=1;i<=n;i++)
        {
            scanf("%s",&s[i]);
            len[i]=strlen(s[i]);
        }
        memset(w,0,sizeof(w));
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=n;j++)
            {
                if(i==j)w[i][j]=0;
                else
                {
                    int x=len[i]-1;
                    int y=0;
                    int tmp=0;
                    while(x>=0&&y<len[j])
                    {
                        if(s[i][x]==s[j][y])
                        {
                            tmp++;
                            x--;
                            y++;
                        }
                        else break;
                    }
                    w[i][j]=tmp;
                }
            }
        }
        printf("%d\n",KM());
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值