/****************************************************************************************************
无聊死了。。。
****************************************************************************************************/
#include <iostream>
#include <functional>
#include <algorithm>
//#include <hash_map> //Visual C++ lib
#include <complex>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <sstream>
#include <utility>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <limits>
#include <memory>
#include <string>
#include <vector>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
using namespace std;
#define LOWBIT(x) ( (x) & ( (x) ^ ( (x) - 1 ) ) )
#define CLR(x, k) memset((x), (k), sizeof(x))
#define CPY(t, s) memcpy((t), (s), sizeof(s))
#define SC(t, s) static_cast<t>(s)
#define LEN(s) static_cast<int>( strlen((s)) )
#define SZ(s) static_cast<int>( (s).size() )
typedef double LF;
//typedef long long LL; //GNU C++
//typedef unsigned long long ULL;
typedef __int64 LL; //Visual C++ 2010
typedef unsigned __int64 ULL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef pair<double, double> PDD;
//typedef hash_map<int, int>::iterator HMI;
typedef map<int, int>::iterator MI;
typedef vector<int>::iterator VI;
typedef list<int>::iterator LI;
typedef set<int>::iterator SI;
template <typename T>
T sqa(const T &x)
{
return x * x;
}
const int INF_INT = 0x3f3f3f3f;
const LL INF_LL = 0x7fffffffffffffffLL; //15f
const double oo = 10e9;
const double eps = 10e-9;
const double PI = acos(-1.0);
const int MAXV = 28;
const int MAXN = 10004;
int test, n, k, m;
int lack;
int lx[MAXV], ly[MAXV], match[MAXV], maze[MAXV][MAXV];
bool visx[MAXV], visy[MAXV];
char tec[MAXN];
bool dfs(const int &x)
{
visx[x] = true;
for (int y = 0; y < 26; ++y)
{
if (visy[y])
{
continue ;
}
int t = lx[x] + ly[y] - maze[x][y];
if (0 == t)
{
visy[y] = true;
if (-1 == match[y] || dfs(match[y]))
{
match[y] = x;
return true;
}
}
else if (lack > t)
{
lack = t;
}
}
return false;
}
int KM()
{
CLR(lx, 0);
CLR(ly, 0);
CLR(match, -1);
for (int i = 0; i < 26; ++i)
{
for (int j = 0; j < 26; ++j)
{
lx[i] = max(lx[i], maze[i][j]);
}
}
for (int x = 0; x < 26; ++x)
{
while (true)
{
CLR(visx, false);
CLR(visy, false);
lack = INF_INT;
if ( dfs(x) )
{
break ;
}
for (int i = 0; i < 26; ++i)
{
if (visx[i])
{
lx[i] -= lack;
}
if (visy[i])
{
ly[i] += lack;
}
}
}
}
int res = 0;
for (int i = 0; i < 26; ++i)
{
res += maze[ match[i] ][i];
}
return res;
}
void ace()
{
char ch;
for (cin >> test; test--; )
{
cin >> n >> k >> m;
for (int i = 0; i < n; ++i)
{
cin >> ch;
tec[i] = ch;
}
while (m--)
{
CLR(maze, 0);
for (int i = 0; i < n; ++i)
{
cin >> ch;
++maze[ tec[i] - 'A' ][ ch - 'A' ];
}
printf("%.4lf\n", KM() * 1.0 / n);
}
}
return ;
}
int main()
{
ace();
return 0;
}
HDU 3718 KM
最新推荐文章于 2015-12-16 19:11:24 发布