HDU 3718 Similarity (二分图最大权匹配 KM)

本文介绍了一种基于最大权匹配算法的问题解决方法,通过将问题转化为二分图上的最大权匹配问题,并使用Kuhn-Munkres (KM) 算法进行求解。该方法应用于计算两个序列间的最大相似度,特别适用于评估学生的分类作业答案。

Similarity

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

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(S i == T i) / L 
L = Length(S) = Length(T), i = 1, 2,... L,
where sum(S i == T i) 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
 

题目大意:根据题目所给公式,求匹配度最高的字符串的匹配度

题目分析:转成二分图最大权匹配,KM搞
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int const MAX = 30;
int const INF = 0x3fffffff;
int n, k, m;
char t[10005][2], s[10005][2];
int mp[MAX][MAX], mt[MAX];
bool visx[MAX], visy[MAX];
int ex_x[MAX], ex_y[MAX], slack[MAX];

bool DFS(int x) {
    visx[x] = true;
    for (int y = 0; y < 26; y++) {
        if (!visy[y]) {
            int gap = ex_x[x] + ex_y[y] - mp[x][y];
            if (gap == 0) {
                visy[y] = true;
                if (mt[y] == -1 || DFS(mt[y])) {
                    mt[y] = x;
                    return true;
                }
            } else {
               slack[y] = min(slack[y], gap);
            }
        }
    }
    return false;
}

int KM() {
    memset(mt, -1, sizeof(mt));
    memset(ex_y, 0, sizeof(ex_y));
    for (int i = 0; i < 26; i++) {
        ex_x[i] = mp[i][0];
        for (int j = 1; j < 26; j++) {
            ex_x[i] = max(ex_x[i], mp[i][j]); 
        }
    }
    for (int i = 0; i < 26; i++) {
        for (int j = 0; j < 26; j++) {
            slack[j] = INF;
        }
        while (true) {
            memset(visx, false, sizeof(visx));
            memset(visy, false, sizeof(visy));
            if (DFS(i)) {
                break;
            }
            int d = INF;
            for (int j = 0; j < 26; j++) {
                if (!visy[j]) {
                    d = min(d, slack[j]);
                }
            }
            for (int j = 0; j < 26; j++) {
                if (visx[j]) {
                    ex_x[j] -= d;
                }
                if (visy[j]) {
                    ex_y[j] += d;
                } else {
                    slack[j] -= d;
                }
            }
        }
    }
    int res = 0;
    for (int i = 0; i < 26; i++) {
        res += mp[mt[i]][i];
    }
    return res;
}

int main() {
    int T;
    scanf("%d", &T);
    while (T --) {
        scanf("%d %d %d", &n, &k, &m);
        for (int i = 0; i < n; i++) {
            scanf("%s", s[i]);
        }
        for (int ca = 0; ca < m; ca++) {
            memset(mp, 0, sizeof(mp));
            for (int i = 0; i < n; i++) {
                scanf("%s", t[i]);
                mp[s[i][0] - 'A'][t[i][0] - 'A'] ++;
            }
            int val = KM();
            printf("%.4f\n", (1.0 * val) / (n * 1.0));
        }
    }
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值