//n an item, using letters to denote the type of each item,//Give a standard answer for each item's letter representation//Give everyone the answer and ask how the match makes two answers more similar//{a a B a b b c c c C},{f F E F e e D D d} denotes an answer//For each letter and letter to build, two answers in the same position between the letters of the weight of the edge plus 1//Then just ask for the maximum right match#include <cstdio>#include <cstring>#include <iostream>using namespace STD;Const intMAXN =10100;Const intINF =0x3f3f3f3f;intw[ -][ -] ;intidx[ -], idy[ -] ;intmatch[ -] ;Charstr[maxn][Ten] ;intN, M, K;intlx[ -], ly[ -] ;intvisx[ -], visy[ -], slack[ -] ;BOOLFindintx) {Visx[x] =1; for(inti =1; I <= k;i++) {if(Visy[i])Continue;intTMP = Lx[x] + ly[i]-w[x][i];if(TMP = =0) {Visy[i] =1;if(Match[i] = =-1|| Find (Match[i])) {match[i] = x;return true; } }ElseSlack[i] = min (slack[i], TMP); }return false;}intKM () {memset(Match,-1,sizeof(match)) ;memset(Ly,0,sizeof(ly)) ; for(inti =1; I <= k;i++) {lx[i] =-inf; for(intj =1; J <= k;j++) lx[i] = max (Lx[i], w[i][j]); } for(inti =1; I <= k;i++) { for(intj =1; J <= k;j++) slack[j] = inf; while(1) {memset(VISX,0,sizeof(VISX)) ;memset(Visy,0,sizeof(Visy)) ;if(Find (i)) Break;intd = inf; for(intj =1; J <= k;j++)if(!visy[j]) d = min (d, slack[j]); for(intj =1; J <= k;j++)if(Visx[j]) lx[j]-= D; for(intj =1; J <= k;j++)if(Visy[j]) ly[j] + = D;ElseSLACK[J]-= D; } }intAns =0; for(inti =1; I <= k;i++) ans + = w[match[i]][i];returnAns;}intMain () {//Freopen ("In.txt", "R", stdin); intTscanf("%d", &t); while(t--) {scanf("%d%d%d", &n, &k, &m);memset(IDX,0,sizeof(IDX));intLen =0; for(inti =1; I <= n;i++) {scanf('%s ', Str[i]);if(!idx[str[i][0]-' A ']) idx[str[i][0]-' A '] = ++len; } while(m--) {Charch[3] ;memset(W,0,sizeof(w));memset(Idy,0,sizeof(Idy)) ; Len =0; for(inti =1; I <= n;i++) {scanf('%s ', ch);if(!idy[ch[0]-' A ']) idy[ch[0]-' A '] = ++len; w[idx[str[i][0]-' A ']][idy[ch[0]-' A ']]++ ; }intans = KM ();printf("%.4lf\n", (Double) ans/(Double) n); } }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
hdu3718similarity km algorithm