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