Test instructions: Tells the weight of the edge between the cards when the first string is reversed, and the second string is the longest common prefix, which is preprocessed under this weight. Under the KM
#include <cstdio>#include <iostream>#include <algorithm>#include <cmath>#include <set>#include <map>#include <string>#include <cstring>#include <stack>#include <queue>#include <vector>#include <cstdlib>#define Lson (rt<<1), L,m#define Rson (rt<<1|1), M+1,r#define M ((l+r) >>1)#define CL (A, B) memset (A,b,sizeof (a));#define LL Long Long#define P pair<int,int>#define X First#define Y Second#define PB Push_back#define FREAD (ZCC) freopen (ZCC, "R", stdin)#define FWRITE (ZCC) freopen (ZCC, "w", stdout)using namespace STD;Const intmaxn=205;Const intinf=1<< -;intW[MAXN][MAXN];intLINKER[MAXN],LX[MAXN],LY[MAXN],SLACK[MAXN];BOOLVISX[MAXN],VISY[MAXN];intNx,ny;BOOLDfsintx) {visx[x]=true; for(intI=0; i<ny;i++) {if(Visy[i])Continue;intTmp=lx[x]+ly[i]-w[x][i];if(tmp==0) {visy[i]=true;if(linker[i]==-1|| DFS (Linker[i])) {linker[i]=x;return true; } }Else if(slack[i]>tmp) {slack[i]=tmp; } }return false;}intKM () {cl (linker,-1); Cl (ly,0); for(intI=0; i<nx;i++) {lx[i]=-inf; for(intj=0; j<ny;j++)if(W[i][j]>lx[i]) {lx[i]=w[i][j];} } for(intx=0; x<nx;x++) {Fill (slack,slack+ny+1, INF); while(true) {cl (VISX,false); CL (Visy,false);if(Dfs (x)) Break;intD=inf; for(intI=0; i<ny;i++)if(!visy[i]&&slack[i]<d) {D=slack[i]; } for(intI=0; i<nx;i++)if(Visx[i]) Lx[i]-=d; for(intI=0; i<ny;i++)if(Visy[i]) Ly[i]+=d;ElseSlack[i]-=d; } }intans=0;BOOLok=false; for(intI=0; i<ny;i++)if(linker[i]!=-1) {Ans+=w[linker[i]][i];if(W[linker[i]][i]==-inf) ok=true;///}if(OK)return 1;returnAns;}Chara[maxn][1005];intGetprefix (Char*s,Char*t) {intn=strlen(s);intm=strlen(t);inti=n-1, j=0;intCnt=0; while(i>=0&&J<M) {if(S[i]==t[j]) cnt++;Else Break; i--;j++; }returnCNT;}intMain () {intN while(~scanf("%d", &n)) { for(intI=0; i<n;i++) {scanf('%s ', &a[i]); } nx=ny=0; for(intI=0; i<n;i++) { for(intj=0; j<n;j++) {if(I==J) w[nx][j]=0;Else{W[nx][j]=getprefix (a[i],a[j]); }} nx++; } ny=n;//printf ("NX =%d, NY =%d\n", nx,ny); printf("%d\n", km ()); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU3722 Card Game (km minimum charge ring overlay)