Topic Link: Click to enter
This topic requires the length of all the same prefixes and suffixes. We can take advantage of the nature of the next array in the KMP algorithm, which is continuously mismatch at the point of Next[len, so that all the same prefix lengths can be obtained. Also note that the length of the whole string can also be regarded as a legal solution.
The code is as follows:
#include <iostream>#include <cstdio>#include <cstring>using namespace STD;Const intmaxn=400000+ -;CharSTR[MAXN];intNEXT1[MAXN];intans[maxn],cnt;voidKMP_NEXT1 (CharX[],intMintNext1[]) {intI,j; j=next1[0]=-1; I=0; while(i<m-1) { while(-1!=j && X[i]!=x[j]) j=next1[j]; Next1[++i]=++j; }/// at Next[m] This point has to be "mismatch". ///In this way, all prefixes with the same suffix can be obtained. while(-1!=J) {if(X[i]==x[j]) ans[cnt++]=j+1; J=NEXT1[J]; }}intMain () {Freopen ("In.txt","R", stdin); while(scanf('%s ', str)!=eof) {intlen=strlen(str); Cnt=0; Kmp_next1 (STR,LEN,NEXT1); for(inti=cnt-1; i>=0; i--)printf("%d", Ans[i]);printf("%d\n", Len); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 2752+kmp+ uses next array properties to find all the same prefixes and suffixes