题目只有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. CDOJ 485 UESTC 485 Game (八数码变形,映射,逆cantor展开)

    题意:八数码,但是转移的方式是转动,一共十二种,有多组询问,初态唯一,终态不唯一. 题解:初态唯一,那么可以预处理出012345678的所有转移情况,然后将初态对012345678做一个映射,再枚举一 ...

  2. ListView适配器Adapter介绍与优化

    一.ListView与Adapter的关系 ListView是Android开发过程中较为常见的组件之一,它将数据以列表的形式展现出来.一般而言,一个ListView由以下三个元素组成: 1.View ...

  3. javascript 完整知识点整理

    by 蔡舒啸 目录 一 5种基本类型 typeof 关键字 三种强制类型转换 日期 二 if语句for语句whiledo-whileswitch-case 比较运算符 逻辑运算符 if for语句 w ...

  4. postman测试传入json

  5. python入门:print打印输出的用法

    #!/usr/bin/python # -*- coding:utf-8 -*- # print打印输出的用法 print("holle,world!") print(1) pri ...

  6. PHP将unicode转utf8最简法

    最近开发时遇到Unicode编码问题,找了半天才知道PHP并没有Unicode转码函数,终于发现用一行PHP代码解决的方案: $str = '{"success":true,&qu ...

  7. Altium Designer入门学习笔记2:使用原创客3D元件库

    请自行淘宝购买: 元件库列表(2018年11月27日): 问题一:在项目库或已安装的库中找不到? 将"原创客"提供的文件全部添加到libraries中!"原创客" ...

  8. visual studio 2013 for windows desk报error MSB8020: The build tools for v141错误

    由于硬件限制,学习在touchgfx暂时在Windows下模拟仿真,了解其基本原理和有一个基本感性认识,因此安装了VS Express 2013 for Desktop轻量级编译器. 有TouchGF ...

  9. Letters CodeForces - 978C (二分)

    Time limit4000 ms Memory limit262144 kB There are nn dormitories in Berland State University, they a ...

  10. 线段树:POJ3468-A Simple Problem with Integers(线段树注意事项)

    A Simple Problem with Integers Time Limit: 10000MS Memory Limit: 65536K Description You have N integ ...