hdu 3722 Card Game(KM算法)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/acm_BaiHuzi/article/details/48374759

Card Game

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1509    Accepted Submission(s): 634


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
 

Source
 

题意:给你n个字符串,任意两个字符串进行互相拼接;例如s1="abcd",s2="dbca",将s1拼接在s2的后面,所得到的值就是将s1翻转得到s1'="dcba",            s1'和s2共有的前缀为1;其值就是1;现在要求的就是讲这些拼起来得到的值为最大。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#define MAXN 310
#define inf 1000000000
#define _clr(x) memset(x,0xff,sizeof(int)*n)

using namespace std;
int n,m;
int mat[MAXN][MAXN];
char s[MAXN*2][1010];
int match1[MAXN],match2[MAXN];

//二分图最佳匹配,kuhn munkras 算法,邻接阵形式,复杂度 O(m*m*n)
//返回最佳匹配值,传入二分图大小 m,n和邻接阵 mat,表示权值
//match1,match2 返回一个最佳匹配,未匹配顶点 match 值为-1
//一定注意 m<=n,否则循环无法终止 //最小权匹配可将权值取相反数
int KM() {
    int s[MAXN],t[MAXN],l1[MAXN],l2[MAXN];
    int p,q,ret=0,i,j,k;
    for (i=0; i<n; i++)
        for (l1[i]=-inf,j=0; j<n; j++)
            l1[i]=mat[i][j]>l1[i]?mat[i][j]:l1[i];
    for (i=0; i<n; l2[i++]=0);
    for (_clr(match1),_clr(match2),i=0; i<n; i++) {
        for (_clr(t),s[p=q=0]=i; p<=q&&match1[i]<0; p++)
            for (k=s[p],j=0; j<n&&match1[i]<0; j++)
                if (l1[k]+l2[j]==mat[k][j]&&t[j]<0) {
                    s[++q]=match2[j],t[j]=k;
                    if (s[q]<0)
                        for (p=j; p>=0; j=p)
                            match2[j]=k=t[j],p=match1[k],match1[k]=j;
                }
        if (match1[i]<0) {
            for (i--,p=inf,k=0; k<=q; k++)
                for (j=0; j<n; j++)
                    if (t[j]<0&&l1[s[k]]+l2[j]-mat[s[k]][j]<p)
                        p=l1[s[k]]+l2[j]-mat[s[k]][j];
            for (j=0; j<n; l2[j]+=t[j]<0?0:p,j++);
            for (k=0; k<=q; l1[s[k++]]-=p);
        }
    }
    for (i=0; i<n; i++)
        ret+=mat[i][match1[i]];
    return ret;
}


int main() {
    //freopen("test.in","r",stdin);
    while(~scanf("%d",&n)) {
        m=n;
        for(int i=0; i<n; i++) {
            scanf("%s",s[i]);
            int l=strlen(s[i]);
            int x=0;
            for(int j=l-1; j>=0; j--)
                s[i+n][x++]=s[i][j];
            s[i+n][l]=0;
        }
        for(int i=0; i<n; i++) {
            for(int j=0; j<n; j++) {
                if(i==j) {
                    mat[i][j]=0;
                    continue;
                }
                int pos=0;
                while(s[i][pos]&&s[j+n][pos]&&s[i][pos]==s[j+n][pos])pos++;
                mat[i][j]=pos;
            }
        }
        printf("%d\n",KM());
    }
    return 0;
}



阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页