【二分图匹配入门专题1】L - Card Game hdu 3722【km算法】

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个字符串中两个字符串相连的权值是串1逆置后和串2的最长公共前缀,自身和自身相连的权值为0,问输出最大完美匹配值。思路:将串1(1~n)从后往前和串2(1~n)进行匹配,匹配值存入两串相连的边(实现:用两个for进行循环匹配),之后就是km算法啦~~我不知道自己哪里来的想法在读入结束条件加一个n!=0,结果一直超时,我各种优化还是超时,到了饭点才发现自己输入的问题,真是一个浪费时间的bug
#include<stdio.h>
#include<string.h>
#define N 210
#define M 1110
#define INF 0x3f3f3f3f

int w[N][N],lx[N],ly[N];
int visx[N],visy[N],linker[N],slack[N];
int n,ans,nx,ny;
char e[N][M];

void Getmap()//建图
{
    int i,j,x,y,count,l;
    for(x = 1; x <= n; x ++)
    {
        for(y = 1; y <= n; y ++)
        {
            if(x == y)
                w[x][y] = 0;
            else
            {
                l = strlen(e[x]+1);
                count = 0;
                for(i = l,j = 1;i > 0&&e[y][j]!=‘\0‘;i--,j++)
                {
                    if(e[x][i] == e[y][j])
                        count++;
                    else
                        break;
                }
                w[x][y] = count;
            }
        }
    }
    return;
}

int dfs(int x)//寻找增广路
{
    int y,tmp;
    visx[x] = 1;
    for(y = 1; y <= ny; y ++)
    {
        if(!visy[y])
        {
            tmp = lx[x] + ly[y] - w[x][y];
            if(!tmp)
            {
                visy[y] = 1;
                if(linker[y] == -1||dfs(linker[y]))
                {
                    linker[y] = x;
                    return 1;
                }
            }
            else if(slack[y] > tmp)
                slack[y] = tmp;
        }
    }
    return 0;
}
int KM()//km算法
{
    int x,y,i,j,sum,d;
    memset(linker,-1,sizeof(linker));
    memset(ly,0,sizeof(ly));

    for(x = 1; x <= nx; x ++)
        for(y = 1,lx[x] = -INF; y <= ny; y ++)
            if(lx[x] < w[x][y])
                lx[x] = w[x][y];

    for(x = 1; x <= nx; x ++)
    {
        for(i = 1; i <= ny; i ++)
            slack[i] = INF;
        while(1)
        {
            memset(visx,0,sizeof(visx));
            memset(visy,0,sizeof(visy));
            d = INF;
            if(dfs(x))
                break;
            for(y = 1; y <= ny; y ++)
                if(!visy[y]&&d > slack[y])
                    d = slack[y];
            for(i = 1; i <= nx; i ++)
                if(visx[i])
                    lx[i] -= d;

            for(i = 1; i <= ny; i ++)
                if(visy[i])
                    ly[i] += d;
                else
                    slack[i]-=d;
        }
    }
    sum = 0;
    for(i = 1; i <= ny; i ++)
        if(linker[i]!=-1)
            sum += w[linker[i]][i];
    return sum;
}

int main()
{
    int i;
    while(scanf("%d",&n)!=EOF)
    {
        getchar();
        nx = ny = n;
        for(i = 1; i <= n; i ++)
            scanf("%s",e[i]+1);
        Getmap();//建图
        ans = KM();//km算法
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-10-07 15:23:15

【二分图匹配入门专题1】L - Card Game hdu 3722【km算法】的相关文章

【二分图匹配入门专题1】D - Matrix hdu2119【最小顶点覆盖】

Give you a matrix(only contains 0 or 1),every time you can select a row or a column and delete all the '1' in this row or this column . Your task is to give out the minimum times of deleting all the '1' in the matrix. InputThere are several test case

【二分图匹配入门专题1】K - Going Home hdu1533【km匹配】

On a grid map there are n little men and n houses. In each unit time, every little man can move one unit step, either horizontally, or vertically, to an adjacent point. For each little man, you need to pay a $1 travel fee for every step he moves, unt

【二分图匹配入门专题1】I - Hiding Gold light oj 1152【二分图匹配】-------------------我是终于不那么水的水题分割线------------------------

You are given a 2D board where in some cells there are gold. You want to fill the board with 2 x 1 dominoes such that all gold are covered. You may use the dominoes vertically or horizontally and the dominoes may overlap. All you have to do is to cov

【二分图匹配入门专题1】E - Air Raid hdu1151【最小路径覆盖】

Consider a town where all the streets are one-way and each street leads from one intersection to another. It is also known that starting from an intersection and walking through town's streets you can never reach the same intersection i.e. the town's

【二分图匹配入门专题1】F - COURSES poj1469【最大匹配--匈牙利算法模板题】

Consider a group of N students and P courses. Each student visits zero, one or more than one courses. Your task is to determine whether it is possible to form a committee of exactly P students that satisfies simultaneously the conditions: every stude

【二分图匹配入门专题1】B - Girls and Boys hdu1068【最大独立集】

the second year of the university somebody started a study on the romantic relations between the students. The relation "romantically involved" is defined between one girl and one boy. For the study reasons it is necessary to find out the maximu

【二分图匹配入门专题1】M - Cyclic Tour hdu1853【km算法--判断自重边】

There are N cities in our country, and M one-way roads connecting them. Now Little Tom wants to make several cyclic tours, which satisfy that, each cycle contain at least two cities, and each city belongs to one cycle exactly. Tom wants the total len

【二分图匹配入门专题1】H - Marriage Media light oj 1184【二分图最大匹配】

You run a marriage media. You take some profiles for men and women, and your task is to arrange as much marriages as you can. But after reading their bio-data you have found the following criteria. No man will marry a woman if their height gap is gre

【二分图匹配入门专题1】 N - Special Fish hdu3395 【km算法】【春风十里,都不如tle~~~】

There is a kind of special fish in the East Lake where is closed to campus of Wuhan University. It's hard to say which gender of those fish are, because every fish believes itself as a male, and it may attack one of some other fish who is believed to