hdu 3718 Similarity【KM匹配】

来源:互联网 发布:polycom软件下载 编辑:程序博客网 时间:2024/05/20 23:08

Similarity

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1915    Accepted Submission(s): 776

Problem Description

When we were children, we were always asked to do the classification homework. For example, we were given words {Tiger, Panda, Potato, Dog, Tomato, Pea, Apple, Pear, Orange, Mango} and we were required to classify these words into three groups. As you know, the correct classification was {Tiger, Panda, Dog}, {Potato, Tomato, Pea} and {Apple, Pear, Orange, Mango}. We can represent this classification with a mapping sequence{A,A,B,A,B,B,C,C,C,C}, and it means Tiger, Panda, Dog belong to group A, Potato, Tomato, Pea are in the group B, and Apple, Pear, Orange, Mango are in the group C.
But the LABEL of group doesn't make sense and the LABEL is just used to indicate different groups. So the representations {P,P,O,P,O,O,Q,Q,Q,Q} and {E,E,F,E,F,F,W,W,W,W} are equivalent to the original mapping sequence. However, the representations {A,A,A,A,B,B,C,C,C,C} and
{D,D,D,D,D,D,G,G,G,G} are not equivalent.



The pupils in class submit their mapping sequences and the teacher should read and grade the homework. The teacher grades the homework by calculating the maximum similarity between pupils' mapping sequences and the answer sequence. The definition of similarity is as follow. 

Similarity(S, T) = sum(Si == Ti) / L 
L = Length(S) = Length(T), i = 1, 2,... L,
where sum(Si == Ti) indicates the total number of equal labels in corresponding positions. The maximum similarity means the maximum similarities between S and all equivalent sequences of T, where S is the answer and fixed. Now given all sequences submitted by pupils and the answer sequence, you should calculate the sequences' maximum similarity.

 

 

Input

The input contains multiple test cases. The first line is the total number of cases T (T < 15). The following are T blocks. Each block indicates a case. A case begins with three numbers n (0 < n < 10000), k (0 < k < 27), m (0 < m < 30), which are the total number of objects, groups, and students in the class. The next line consists of n labels and each label is in the range [A...Z]. You can assume that the number of different labels in the sequence is exactly k. This sequence represents the answer. The following are m lines, each line contains n labels and each label also is in the range [A...Z]. These m lines represent the m pupils' answer sequences. You can assume that the number of different labels in each sequence doesn't exceed k.

 

 

Output

For each test case, output m lines, each line is a floating number (Round to 4 digits after the decimal point). You should output the m answers in the order of the sequences appearance.

 

 

Sample Input

2

10 3 3

A A B A B B C C C C

F F E F E E D D D D

X X X Y Y Y Y Z Z Z

S T R S T R S T R S

3 2 2

A B A

C D C

F F E

 

 

Sample Output

1.0000

0.7000

0.5000

1.0000

0.6667

 

 

Author

LIN, Yue

 

 

Source

2010 Asia Chengdu Regional Contest

 

题目大意:第一行输入三个元素:

n,k,m表示序列长度为n,保证序列中最多有k种字母,然后有m份答案(每份答案的字母可以随意转换,但是一种字母只能换成一种字母),问其正确率最大可能为多少

 

思路:


1、首先想到二部图,将给出的m份答案中的字母看成左集合,将26个字母看成右集合,转换字母问题变成了匹配问题。


2、那么匹配所带来的收益又是多大呢?那么我们这里就需要在二分匹配的基础上引入一个权值匹配的问题,比如样例中:

正确答案: A B A

给出答案:C D C

很明显可以看出,将字母C匹配上字母A(将答案中的字母C变成字母A所需带来的收益/能够正确对上的字符数)为2,将字母C匹配上字母B为0,字母D匹配上字母A为0,字母D匹配上字母B为1。


3、那么建图方式就清晰明了了,将答案字母修改成对应字母能够对上几个正确答案(比方说x个),那么map【i】【j】=x即可。

那么建好图之后跑一遍KM,就是能够得到最大匹配值,对应ans=最大匹配/n


Ac代码:


#include<stdio.h>#include<string.h>#include<iostream>using namespace std;#define inf 0x3f3f3f3fint map[30][30];char a[100000];char b[100000];int lx[50];int ly[50];int vx[50];int vy[50];int match[50];int low;int vis[50];int n,kk,m;void getmap(){    memset(map,0,sizeof(map));    for(int i=0;i<n;i++)    {        int u=a[i]-'A'+1;        int v=b[i]-'A'+1;        map[v][u]++;    }}int find(int u){    vx[u]=1;    for(int j=1;j<30;j++)    {        if(vy[j]==1)continue;        int tmp=lx[u]+ly[j]-map[u][j];        if(tmp==0)        {            vy[j]=1;            if(match[j]==-1||find(match[j]))            {                match[j]=u;                return 1;            }        }        else if(tmp<low)low=tmp;    }    return 0;}void KM(){    memset(match,-1,sizeof(match));    memset(lx,0,sizeof(lx));    memset(ly,0,sizeof(ly));    for(int i=1;i<30;i++)    {        for(int j=1;j<30;j++)        {            lx[i]=max(lx[i],map[i][j]);        }    }    for(int i=1;i<30;i++)    {        while(1)        {            memset(vx,0,sizeof(vx));            memset(vy,0,sizeof(vy));            low=inf;            if(find(i))break;            for(int j=1;j<30;j++)            {                if(vx[j])lx[j]-=low;                if(vy[j])ly[j]+=low;            }        }    }    int ans=0;    for(int i=1;i<30;i++)    {        ans+=map[match[i]][i];    }    printf("%.4lf\n",ans*1.0/n*1.0);}int main(){    int t;    scanf("%d",&t);    while(t--)    {        scanf("%d%d%d",&n,&kk,&m);        for(int i=0;i<n;i++)        {            cin>>a[i];        }        while(m--)        {            for(int j=0;j<n;j++)            {                cin>>b[j];            }            getmap();            KM();        }    }}



0 0