zoukankan      html  css  js  c++  java
  • hdu 3718(KM算法)

    比较基础的题了。 构图模板

    Similarity

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


    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
     
    Recommend
    zhouzeyong
     
    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    using namespace std;
    #define INF 0x3fffffff
    #define N 33
    int link[30],link1[30];
    char tlink[30];
    int g[30][30];
    int len,k,q;
    char str[10010],str1[10010];
    int n,m;
    int markx[N],marky[N];
    int mark[N];
    int wx[N],wy[N];
    int save[N];
    int pre[N];
    
    int dfs(int s)
    {
        markx[s]=1;
        for(int i=1;i<=m;i++)
        {
            if(wx[s]+wy[i]-g[s][i]<save[i]) save[i]=wx[s]+wy[i]-g[s][i];
            if(mark[i]==1||wx[s]+wy[i]!=g[s][i]) continue;
            mark[i]=1;
            marky[i]=1;
            if(pre[i]==-1||dfs(pre[i]))
            {
                pre[i]=s;
                return 1;
            }
        }
        return 0;
    }
    
    int KM()
    {
        memset(pre,-1,sizeof(pre));
        memset(wx,0,sizeof(wx));
        memset(wy,0,sizeof(wy));
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
                wx[i]=max(wx[i],g[i][j]);
        for(int i=1;i<=n;i++)
        {
            while(1)
            {
                memset(markx,0,sizeof(markx));
                memset(marky,0,sizeof(marky));
                memset(mark,0,sizeof(mark));
                for(int j=1;j<=m;j++)
                    save[j]=INF;
                if(dfs(i)==1) break; //因为n是少的,所以可能出现死循环
                int mi=INF;
                for(int j=1;j<=m;j++)
                    if(marky[j]==0&&save[j]<mi) mi=save[j];
                for(int j=1;j<=n;j++) 
                    if(markx[j]==1) wx[j]-=mi;
                for(int j=1;j<=m;j++)
                    if(marky[j]==1) wy[j]+=mi;
            }
        }
        int sum=0;
        for(int i=1;i<=m;i++)
            if(pre[i]!=-1)
            sum+=g[ pre[i] ][i];
        return sum;
    }
    
    int main()
    {
        int T;
        scanf("%d",&T);
        while(T--)
        {
            memset(link,0,sizeof(link));
            scanf("%d%d%d",&len,&k,&q);
            for(int i=0;i<len;i++)
                cin>>str[i];
            m=0;
            for(int i=0;i<len;i++)
            {
                if(link[str[i]-'A']==0)
                {
                    link[str[i]-'A'] = ++m;
                    tlink[m]=str[i];
                }
            }
            for(int i=0;i<q;i++)
            {
                memset(g,0,sizeof(g));
                memset(link1,0,sizeof(link1));
                for(int j=0;j<len;j++)
                    cin>>str1[j];
                n=0;
                for(int i1=1;i1<=m;i1++)
                    for(int j=0;j<len;j++)
                    {
                        if(link1[ str1[j]-'A' ]==0) link1[ str1[j]-'A' ]=++n;
                        if( str[j] == tlink[i1] )
                            g[ link1[ str1[j]-'A' ] ][ i1 ]++;
                    }
                printf("%.4lf\n",(double)KM()/(double)len);
            }
        }
        return 0;
    }
  • 相关阅读:
    C++下遍历文件夹
    pycharm入门的简易使用教程
    easyUI—— datagrid 日期比较天数,小时
    Js获取当前日期时间+日期印证+判断闰年+日期的天数差+日期格式化+JS判断某年某月有多少天
    js获取一个月份最大天数和获取月的最后一天
    根据样式往里添加动态数据
    在同一个数据库表中添加不同的数据(笛卡尔积)
    修改某个数据可属性值根据三层 BLL
    根据条件删除
    xmlHttp.status的值(HTTP状态表)
  • 原文地址:https://www.cnblogs.com/chenhuan001/p/3064150.html
Copyright © 2011-2022 走看看