Topic Links:
http://acm.hdu.edu.cn/showproblem.php?pid=3722
1 /*2 problem3 match any of the two strings to match the weighted value and the maximum4 5 Thinking of solving problems6 calculates the weights of any string, using the KM algorithm. 7 */ 8#include <cstdio>9#include <cstring>Ten#include <algorithm> One using namespacestd; A - Const intmaxn= About; - intW[maxn][maxn],n; the Chars[maxn][1100]; - intLX[MAXN],LY[MAXN]; - intLEFT[MAXN]; - BOOLS[MAXN],T[MAXN]; + intLEN[MAXN]; - + voidpresolve (); A intKM (); at BOOLMatchinti); - voidupdate (); - - intMain () - { - inti,j; in while(SCANF ("%d", &n)! =EOF) - { to for(i=1; i<=n;i++) + { -scanf"%s", S[i]); thelen[i]=strlen (S[i]); * } $ presolve ();Panax Notoginseng -printf"%d\n", KM ()); the } + } A the BOOLMatchinti) + { -s[i]=true; $ for(intj=1; j<=n;j++)if(Lx[i]+ly[j] = = W[i][j] &&!T[j]) { $t[j]=true; - if(!left[j] | |match (Left[j])) { -left[j]=i; the return true; - }Wuyi } the return false; - } Wu - voidUpdate () About { $ intA=1<< -; - for(intI=1; i<=n; i++){ - if(S[i]) { - for(intj=1; j<=n;j++){ A if(!T[j]) { +A = min (A,lx[i]+ly[j]-w[i][j]); the } - } $ } the } the the for(intI=1; i<=n;i++){ the if(S[i]) lx[i]-=A; - if(T[i]) ly[i] + =A; in } the } the About intKM () the { the for(intI=1; i<=n;i++){ theLeft[i] = Lx[i] = Ly[i] =0; + for(intj=1; j<=n; J + +) -lx[i]=Max (lx[i],w[i][j]); the }Bayi the for(intI=1; i<=n; i++){ the for(;;) { - for(intj=1; j<=n;j++){ -s[j]=t[j]=0; the } the if(Match (i)) Break; the Elseupdate (); the } - } the the intans=0; the for(intI=1; i<=n;i++)94Ans + =W[left[i]][i]; the returnans; the } the 98 voidpresolve () About { - inti,j,k,p1,p2,cnt;101 for(i=1; i<=n;i++)102 {103 for(j=1; j<=n;j++)104 { the if(i==j)106 {107w[i][j]=0;108 Continue ;109 } the 111Cnt=0; thep1=len[i]-1;113P2=0; the while(1) the { the if(S[I][P1]==S[J][P2]) cnt++;117 Else Break ;118P1--, p2++;119 if(p1<0|| P2>=LEN[J]) Break ; - }121w[i][j]=CNT;122 }123 }124}
HDU 3722 Card Game (binary graph best perfect match +km algorithm)