Seek the Name, Seek the Fame POJ, famepoj
Seek the Name, Seek the Fame POJ-2752
Http://972169909-qq-com.iteye.com/blog/1071548
(Simple application of kmp next)
Simply put, this question is to find all strings with the same prefix and suffix.
Find the largest suffix with the same prefix for the first time, and then find the largest suffix. The prefix of the same prefix and suffix must be prefixed before the largest and the same prefix and suffix, the suffix with the same prefix and suffix must be a section after the suffix with the same largest prefix and suffix. Because it is a suffix with the same prefix, move the section after the suffix to the end of the prefix to get it, that is, retrieve its largest and identical prefix and Suffix from the prefix with the same largest prefix and suffix. This process is recursive until the maximum length of the suffix with the same prefix is 0.
Of course, the next of kmp does not leave traces of the entire string length m, so m must be output separately.
1 #include<cstdio> 2 #include<cstring> 3 char s[1001000]; 4 int f[1001000]; 5 int m; 6 int getf() 7 { 8 int i=0,j=f[0]=-1; 9 while(i<m)10 {11 while(j>=0&&s[i]!=s[j])12 {13 j=f[j];14 }15 i++;j++;16 f[i]=j;17 }18 }19 void print(int i)20 {21 if(f[i]>0) print(f[i]),printf("%d ",f[i]);22 }23 int main()24 {25 while(scanf("%s",s)==1)26 {27 m=strlen(s);28 getf();29 print(m);30 printf("%d\n",m);31 }32 return 0;33 }