Jimmy invents an interesting card game. There is N cards, each of which contains a string Si. Jimmy wants to stick them into several circles, and all card belongs to one circle exactly. When sticking and cards, Jimmy would get a score. The score of sticking, cards is the longest common prefix of the second card and the reverse of the first card. For example, if Jimmy sticks the card S1 containing ' ABCD ' in front of the ' card S2 containing ' Dcab ', the score is 2. And if Jimmy sticks S2 in front of S1, the score is 0. The card can also stick to itself to form a self-circle, whose score is 0.
For example, there is 3 cards, whose strings is s1= "AB", s2= "Bcc", s3= "CCB". There is 6 possible sticking:
1. S1->s2, S2->S3, S3->S1, the score is 1+3+0 = 4
2. S1->s2, S2->S1, S3->S3, the score is 1+0+0 = 1
3. S1->s3, S3->S1, S2->S2, the score is 0+0+0 = 0
4. S1->s3, S3->s2, S2->S1, the score is 0+3+0 = 3
5. S1->s1, S2->s2, S3->S3, the score is 0+0+0 = 0
6. S1->s1, S2->S3, S3->S2, the score is 0+3+3 = 6
The best score is 6.
Given The information of all the cards and help Jimmy find the best possible score.
Inputthere is several test cases. The first line of all test case contains an integer n (1 <= n <= 200). Each of the next N lines contains a string Si. You can assume the strings contain alphabets (' A ' – ' Z ', ' a '-' z ') only, and the length of every string was no more than 1000.
Outputoutput one line for each test case, indicating the corresponding answer. Sample Input
3abbccccb1abcd
Sample Output
60
Test instructions: give you n strings, n strings of two strings connected to the weight is the string 1 inverse and string 2 the longest public prefix, itself and the value of the connection itself is 0, ask the output maximum perfect match value.
Idea: String 1 (1~n) is matched from back-to-front and String 2 (1~n), the matching value is stored in two strings of connected edges (implemented: loop matching with two for), then the KM algorithm ~ ~
I don't know where I came from. The idea of reading into the end of the condition plus a n!=0, the result has been timed out, my various optimizations or timeouts, to the point of the meal to find their input problems, it is a waste of time of the bug
#include <stdio.h>#include<string.h>#defineN 210#defineM 1110#defineINF 0x3f3f3f3fintW[n][n],lx[n],ly[n];intVisx[n],visy[n],linker[n],slack[n];intN,ans,nx,ny;CharE[n][m];voidGetmap ()//Building Map{ inti,j,x,y,count,l; for(x =1; x <= N; X + +) { for(y =1; Y <= N; Y + +) { if(x = =y) w[x][y]=0; Else{L= strlen (e[x]+1); Count=0; for(i = L,j =1; i >0&&e[y][j]!=' /'; I--, J + +) { if(E[x][i] = =E[y][j]) Count++; Else Break; } W[x][y]=count; } } } return; }intDfsintX//find an augmented path{ inty,tmp; VISX[X]=1; for(y =1; Y <= ny; Y + +) { if(!Visy[y]) {tmp= Lx[x] + ly[y]-W[x][y]; if(!tmp) {Visy[y]=1; if(Linker[y] = =-1||DFS (Linker[y])) {Linker[y]=x; return 1; } } Else if(Slack[y] >tmp) Slack[y]=tmp; } } return 0;}intKM ()//km algorithm{ intx,y,i,j,sum,d; MEMSET (linker,-1,sizeof(linker)); memset (ly,0,sizeof(ly)); for(x =1; x <= NX; X + +) for(y =1, lx[x] =-inf; Y <= ny; Y + +) if(Lx[x] <W[x][y]) lx[x]=W[x][y]; for(x =1; x <= NX; X + +) { for(i =1; I <= NY; i + +) Slack[i]=INF; while(1) {memset (VISX,0,sizeof(VISX)); memset (Visy,0,sizeof(Visy)); D=INF; if(Dfs (x)) Break; for(y =1; Y <= ny; Y + +) if(!visy[y]&&d >Slack[y]) d=Slack[y]; for(i =1; I <= NX; i + +) if(Visx[i]) lx[i]-=D; for(i =1; I <= NY; i + +) if(Visy[i]) ly[i]+=D; ElseSlack[i]-=D; }} sum=0; for(i =1; I <= NY; i + +) if(linker[i]!=-1) Sum+=W[linker[i]][i]; returnsum;}intMain () {inti; while(SCANF ("%d", &n)! =EOF) {GetChar (); NX= NY =N; for(i =1; I <= N; i + +) scanf ("%s", e[i]+1); Getmap ();//Building Mapans = KM ();//km algorithmprintf"%d\n", ans); } return 0;}
"Binary Map matching Introductory topic 1" L-card Game hdu 3722 "km algorithm"