比较基础的题了。 构图模板
Similarity
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1076 Accepted Submission(s): 411
But the LABEL of group doesn't make sense and the LABEL is just used to indicate different groups. So the representations {P,P,O,P,O,O,Q,Q,Q,Q} and {E,E,F,E,F,F,W,W,W,W} are equivalent to the original mapping sequence. However, the representations {A,A,A,A,B,B,C,C,C,C} and
{D,D,D,D,D,D,G,G,G,G} are not equivalent.

The pupils in class submit their mapping sequences and the teacher should read and grade the homework. The teacher grades the homework by calculating the maximum similarity between pupils' mapping sequences and the answer sequence. The definition of similarity is as follow.
Similarity(S, T) = sum(Si == Ti) / L
L = Length(S) = Length(T), i = 1, 2,... L,
where sum(Si == Ti) indicates the total number of equal labels in corresponding positions. The maximum similarity means the maximum similarities between S and all equivalent sequences of T, where S is the answer and fixed. Now given all sequences submitted by pupils and the answer sequence, you should calculate the sequences' maximum similarity.
#include <stdio.h> #include <string.h> #include <iostream> using namespace std; #define INF 0x3fffffff #define N 33 int link[30],link1[30]; char tlink[30]; int g[30][30]; int len,k,q; char str[10010],str1[10010]; int n,m; int markx[N],marky[N]; int mark[N]; int wx[N],wy[N]; int save[N]; int pre[N]; int dfs(int s) { markx[s]=1; for(int i=1;i<=m;i++) { if(wx[s]+wy[i]-g[s][i]<save[i]) save[i]=wx[s]+wy[i]-g[s][i]; if(mark[i]==1||wx[s]+wy[i]!=g[s][i]) continue; mark[i]=1; marky[i]=1; if(pre[i]==-1||dfs(pre[i])) { pre[i]=s; return 1; } } return 0; } int KM() { memset(pre,-1,sizeof(pre)); memset(wx,0,sizeof(wx)); memset(wy,0,sizeof(wy)); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) wx[i]=max(wx[i],g[i][j]); for(int i=1;i<=n;i++) { while(1) { memset(markx,0,sizeof(markx)); memset(marky,0,sizeof(marky)); memset(mark,0,sizeof(mark)); for(int j=1;j<=m;j++) save[j]=INF; if(dfs(i)==1) break; //因为n是少的,所以可能出现死循环 int mi=INF; for(int j=1;j<=m;j++) if(marky[j]==0&&save[j]<mi) mi=save[j]; for(int j=1;j<=n;j++) if(markx[j]==1) wx[j]-=mi; for(int j=1;j<=m;j++) if(marky[j]==1) wy[j]+=mi; } } int sum=0; for(int i=1;i<=m;i++) if(pre[i]!=-1) sum+=g[ pre[i] ][i]; return sum; } int main() { int T; scanf("%d",&T); while(T--) { memset(link,0,sizeof(link)); scanf("%d%d%d",&len,&k,&q); for(int i=0;i<len;i++) cin>>str[i]; m=0; for(int i=0;i<len;i++) { if(link[str[i]-'A']==0) { link[str[i]-'A'] = ++m; tlink[m]=str[i]; } } for(int i=0;i<q;i++) { memset(g,0,sizeof(g)); memset(link1,0,sizeof(link1)); for(int j=0;j<len;j++) cin>>str1[j]; n=0; for(int i1=1;i1<=m;i1++) for(int j=0;j<len;j++) { if(link1[ str1[j]-'A' ]==0) link1[ str1[j]-'A' ]=++n; if( str[j] == tlink[i1] ) g[ link1[ str1[j]-'A' ] ][ i1 ]++; } printf("%.4lf\n",(double)KM()/(double)len); } } return 0; }