hdu2594 求s1前缀与s2后缀最长重合串
还是没能熟练掌握巧妙的next数组,把s2接到s1后面,求next数组
当next[n]>a||next[b]>b继续求next直到为0或者不满足这两种
此时next[n]值就是最大重复串
1 #include<stdio.h> 2 #include<string.h> 3 int next[100005]; 4 char s1[100005],s2[100005]; 5 void getnext(int n) 6 { 7 int i,j; 8 next[1]=j=0; 9 for (i=2;i<=n;i++) 10 { 11 while (j>0&&s1[j+1]!=s1[i]) j=next[j]; 12 if (s1[j+1]==s1[i]) j++; 13 next[i]=j; 14 } 15 } 16 int main() 17 { 18 int len1,len2,n,i; 19 while (~scanf("%s%s",s1,s2)) 20 { 21 len1=strlen(s1); len2=strlen(s2); 22 strcat(s1,s2); 23 n=len1+len2; 24 for (i=n;i>=1;i--) 25 s1[i]=s1[i-1]; 26 getnext(n); 27 while (next[n]!=0&&(next[n]>len1||next[n]>len2)) 28 n=next[n]; 29 if (next[n]==0) printf("0\n"); 30 else{ 31 for (i=1;i<=next[n];i++) printf("%c",s1[i]); 32 printf(" %d\n",next[n]); 33 } 34 } 35 return 0; 36 }