Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=5510
Ideas:
First directly with KMP rash hair, timed out, and later found that if the preceding string is included in the following string, then we do not need to use the preceding string to compare, mark him off.
Implementation code:
#include <iostream>#include<algorithm>#include<cstring>#include<cstdio>using namespacestd;Chars[505][2050];intvis[510];intnext1[2050];intSlen,tlen;voidGet_next (Char*mat) { intj,k; Tlen=strlen (MAT); J=0; k=-1; next1[0]=-1; while(j<Tlen) { if(k==-1|| mat[j]==mat[k]) next1[++j]=++K; Elsek=Next1[k]; }}intKmp_pos (Char*STR,Char*mat) { intI=0, j=0; Slen=strlen (str); Get_next (MAT); while(i<slen&&j<Tlen) { if(j==-1|| str[i]==Mat[j]) {i++;j++; } ElseJ=Next1[j]; } if(j==Tlen)returnI-Tlen; return-1;}intMain () {intTt,n; intttt=0; scanf ("%d",&TT); while(tt--) {TTT++; intsum=-1; scanf ("%d",&N); for(intI=1; i<=n;i++) {scanf ("%s", S[i]); Vis[i]=1; } intflag=0; intSt =1; for(inti =2; I <= N;i + +){ intAns=kmp_pos (s[i],s[i-1]); if(ans! =-1) Vis[i-1] =0; } for(inti=n;i>=1; i--) {flag=0; for(intj=i-1; j>=1; j--) { if(vis[j]==0)Continue; intans=Kmp_pos (S[i],s[j]); if(ans==-1) {flag=1; Break; } } if(flag==1) {sum=i; Break; }} printf ("Case #%d:", TTT); printf ("%d\n", sum); }}
HDU 5510 Bazinga (kmp+ violence marker)