zoukankan      html  css  js  c++  java
  • HDU-3718 Similarity

    题目只有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
    ", double(km())/n);
    		}
    	}
    	return 0;
    } 
    

      

  • 相关阅读:
    无监督学习在反欺诈中的应用
    Java中Object对象方法
    ambari下httpfs安装
    深入理解spark-rdd详解
    Tensorflow实践Basic Classification
    深入理解spark-两种调度模式FIFO,FAIR模式
    深入理解spark-taskScheduler,schedulerBackend源码分析
    js模仿页面点击
    记一次请求走私学习
    十种常见的报错注入
  • 原文地址:https://www.cnblogs.com/NanoApe/p/4382065.html
Copyright © 2011-2022 走看看