Seek the Name, Seek the Fame POJ, famepoj

Source: Internet
Author: User

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 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.