hdu 3722 Card Game【KM】

版权声明:0.0小白有写的不好不对的地方请指出~ https://blog.csdn.net/mengxiang000000/article/details/52596094

Card Game

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

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

2010 Asia Tianjin Regional Contest

Recommend

zhouzeyong   |   We have carefully selected several similar problems for you:  3720 3721 3723 3724 3725 

 

题目大意:

每一个字符串都需要去接另外一个字符串,每个字符串保证只存在一个环中,问一个最大权值设定方案,

对应两个字符串想要互相连接,得到的权值是这样判定的:

abc连接cba,abc翻转之后是cba,和cba每个字母都对应上了,值为3.abc连接cda,abc翻转之后是cba,和cda的第一个字母对上了,虽然第三个字母也对上了,但是中间间隔了一个没有对上的字符,那么值为1.


思路:


1、首先n^2设定一下每个字符串和其他字符串连接能够得到的权值。


2、然后根据这个权值,我们直接KM匹配即可,得到的最大值,就是最优解。


Ac代码:


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int map[300][300];
char a[300][2000];
int match[350];
int lx[350];
int ly[350];
int vx[350];
int vy[350];
int n,low;
int find(int u)
{
    vx[u]=1;
    for(int i=1;i<=n;i++)
    {
        if(vy[i]==1)continue;
        int tmpp=lx[u]+ly[i]-map[u][i];
        if(tmpp==0)
        {
            vy[i]=1;
            if(match[i]==-1||find(match[i]))
            {
                match[i]=u;
                return 1;
            }
        }
        else if(tmpp<low)low=tmpp;
    }
    return 0;
}
void KM()
{
    memset(match,-1,sizeof(match));
    memset(lx,0,sizeof(lx));
    memset(ly,0,sizeof(ly));
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            lx[i]=max(lx[i],map[i][j]);
        }
    }
    for(int i=1;i<=n;i++)
    {
        while(1)
        {
            memset(vx,0,sizeof(vx));
            memset(vy,0,sizeof(vy));
            low=0x3f3f3f3f;
            if(find(i))break;
            for(int j=1;j<=n;j++)
            {
                if(vx[j])lx[j]-=low;
                if(vy[j])ly[j]+=low;
            }
        }
    }
    int sum=0;
    for(int i=1;i<=n;i++)
    {
        sum+=map[match[i]][i];
    }
    printf("%d\n",sum);
}
void getmap()
{
    memset(map,0,sizeof(map));
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(i==j)continue;
            int leni=strlen(a[i]);
            reverse(a[i],a[i]+leni);
            int lenj=strlen(a[j]);
            int minn=max(leni,lenj);
            for(int k=0;k<minn;k++)
            {
                if(a[i][k]==a[j][k])
                {
                    map[i][j]++;
                }
                else break;
            }
            reverse(a[i],a[i]+leni);
        }
    }
}
int main()
{
    while(~scanf("%d",&n))
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%s",a[i]);
        }
        getmap();
        KM();
    }
}



阅读更多

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