Poj--2752seek the Name, Seek the application of the Fame kmpnext array

Source: Internet
Author: User

The title can be replaced by the length of all suffixes with the same prefix as the end of the last character, so you need to take advantage of the properties of the next array in the KMP function.
We can first find the next value for each of the first n-1 words and then find all possible (not longest) next values for the nth letter.
Of course we can first find the next value of all the letters, and then sweep it backwards again, that is, Next[len],next[next[len]. This also allows for all lengths to be calculated.
Note that each string itself is to meet certain requirements.

The code is as follows:

#include <iostream>#include <cstdio>#include <cstring>using namespace Std;Char Str[500000];intnext[500000];intans[500000];intCntvoidGetnext () {intj=0;intN=strlen (Str+1); next[1]=0; for(intI=2; i<=n;i++) {if(i==n) { while(j>0)            {if(Str[j+1]==Str[i]) ans[cnt++]=j+1;            J=NEXT[J]; }if(Str[j+1]==Str[i]) ans[cnt++]=j+1; } while(j>0&&Str[j+1]!=Str[i]) j=next[j];if(Str[j+1]==Str[i]) j + +;    Next[i]=j; }}intMain () { while(SCANF ('%s ',Str+1) {!=eof) {cnt=0; Ans[cnt++]=strlen (Str+1); Getnext (); for(inti=cnt-1; i>=0; i--) printf ("%d", Ans[i]); printf"\ n"); }return 0;}

Poj--2752seek the Name, Seek the application of the Fame kmpnext array

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.