题目只有26个字母,所以我们新建一个二分图,v[i][j]表示字母i对应字母j时能成功匹配的个数,给这个边矩阵v求个最大匹配就是答案。

#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <fstream>
#include <iostream>
#include <vector>
#include <cctype>
#define rep(i, l, r) for(int i=l; i<=r; i++)
#define clr(x, c) memset(x, c, sizeof(x))
#define N 12345
#define MAX 1<<30
#define ll long long
using namespace std;
int read()
{
int x=0, f=1; char ch=getchar();
while (!isdigit(ch)) { if (ch=='-') f=-1; ch=getchar(); }
while (isdigit(ch)) { x=x*10+ch-'0'; ch=getchar(); }
return x*f;
}
int readch()
{
char ch=getchar();
while (!isupper(ch)) ch=getchar();
return ch-'A'+1;
} int n, m, l[27], st[27], lx[27], ly[27], v[27][27], k[N];
bool vx[27], vy[27]; bool Find(int x)
{
vx[x]=1;
rep(y, 1, 26)
{
if (vy[y]) continue;
int a=lx[x]+ly[y]-v[x][y];
if (!a)
{
vy[y]=1; if (!l[y] || Find(l[y])) { l[y]=x; return 1; }
}
else st[y]=min(st[y], a);
}
return false;
} inline int km()
{
clr(ly, 0); clr(l, 0); rep(i, 1, 26) lx[i]=-MAX;
rep(i, 1, 26) rep(j, 1, 26) if (lx[i]<v[i][j]) lx[i]=v[i][j];
rep(i, 1, 26)
{
rep(j, 1, 26) st[j]=MAX;
while (1)
{
clr(vx, 0); clr(vy, 0);
if (Find(i)) break; int a=MAX;
rep(j, 1, 26) if (!vy[j] && st[j]<a) a=st[j];
rep(j, 1, 26) if (vx[j]) lx[j]-=a;
rep(j, 1, 26) if (vy[j]) ly[j]+=a; else st[j]-=a;
}
}
int a=0; rep(i, 1, 26) a+=lx[i]+ly[i];
return a;
} int main()
{
int t=read(); while (t--)
{
n=read(); m=read(); m=read();
rep(i, 1, n) k[i]=readch();
rep(o, 1, m)
{
rep(i, 1, 26) rep(j, 1, 26) v[i][j]=0;
rep(i, 1, n) v[k[i]][readch()]++;
printf("%.4lf\n", double(km())/n);
}
}
return 0;
}

  

HDU-3718 Similarity的更多相关文章

  1. HDU 3718 Similarity(KM最大匹配)

    HDU 3718 Similarity 题目链接 题意:给定一个标准答案字符串,然后以下每一行给一个串.要求把字符一种相应一种,要求匹配尽量多 思路:显然的KM最大匹配问题,位置相应的字符连边权值+1 ...

  2. hdu 3718

    一个二分图最大匹配的题: 匈牙利算法不熟: 建了个模,用最小费用最大流解决了 #include <iostream> #include <cstring> #define IN ...

  3. hdu 3718 Different Division

    Different Division Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  4. KM HDU 3718

    #include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> ...

  5. 【转载】图论 500题——主要为hdu/poj/zoj

    转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  6. hdu图论题目分类

    =============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...

  7. HDU图论题单

    =============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...

  8. 【转】KM匹配题集

    转自:http://blog.csdn.net/shahdza/article/details/7779324 [HDU]2255 奔小康赚大钱 模板题★1533 Going Home 模板题★242 ...

  9. [HDOJ3718]Similarity(KM算法,二分图最大匹配)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3718 题意:有一堆答题情况和正确答案,问每一个答题情况的正确率最大是多少. 给每一对答案和答题情况的字 ...

  10. HDU 3316 My Brute(二维费用流)经典

    My Brute Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

随机推荐

  1. 最全面的 python 字符串拼接总结(带注释版)

    在 Python 中字符串连接有多种方式,这里简单做个总结,应该是比较全面的了,方便以后查阅. 加号连接 第一种,通过+号的形式: >>> a, b = 'hello', ' wor ...

  2. WinForm中Timer倒计时

    添加一个Timer控件: 在初始化代码中 public Form1() { InitializeComponent(); button_Read.Enabled = false; button_Sta ...

  3. Unity3d 中键值监听方法

    unity3d的api中没有负责监听键值的方法,不过unity的input类是通过c#类获取各类监听事件,所以我们可以通过c#类监听,方法如下: void OnGUI() { Event e = Ev ...

  4. lua 分割字符串

    -- 参数:待分割的字符串,分割字符 -- 返回:子串表.(含有空串) function split(str, split_char) local sub_str_tab = {} while tru ...

  5. Virt-install用法:

       #一般选项:指定虚拟机的名称.内存大小.VCPU个数及特性等 -n  NAME,  --name=NAME:虚拟机名称,需全局惟一: -r  MEMORY,  --ram=MEMORY:虚拟机内 ...

  6. 对象、句柄、ID之间的区别

    对象是C++的概念,C++的类对象 句柄是Windows SDK的概念,指向某种资源的一种“指针”(有时候底层不一定是指针) 资源ID在MFC里仅仅是一个宏,也就是个整数. 其实,句柄是控件在数据结构 ...

  7. python特殊字符转义符号表示

  8. MySQL查询时,查询结果如何按照where in数组排序

    MySQL查询时,查询结果如何按照where in数组排序 在查询中,MySQL默认是order by id asc排序的,但有时候需要按照where in 的数组顺序排序,比如where in的id ...

  9. 面试题--如何防止sql注入,使用PreparedStatement的预编译,传入的内容就不会和原来的语句发生任何匹配的关系,达到防止注入的方法

    PreparedStatement的用法 jdbc(java database connectivity,java数据库连接)的api中的主要的四个类之一的java.sql.statement要求开发 ...

  10. Java-framework-Vaadin

    安装vaadin: (1) 首先试了maven+vaadin. 安装maven: 1. unzip apache-maven-3.3.9-bin.zip 2. modify PATH environm ...