Topic Link: Click to enter
is actually the simple application of the KMP algorithm next array. If we set this string to a minimum period of x length Len, then the meaning of the next array, we know that the value of Len-next[len] will be equal to X. This is the key point of this topic.
The code is as follows:
#include <iostream>#include <cstdio>#include <cstring>using namespace STD;Const intmaxn=1000000+ -;CharSTR[MAXN];intNEXT[MAXN];intKmp_next (CharX[],intMintNext[]) {intI,j; j=next[0]=-1; I=0; while(i<m) { while(-1!=J&&X[I]!=X[J]) j=next[j]; Next[++i]=++j;the///next function starts from the first bit}if(m% (m-next[m]) = =0)returnm/(M-next[m]);return 1;}intMain () {///freopen ("In.txt", "R", stdin); while(scanf('%s ', str)) {if(str[0]=='. ') Break;printf("%d\n", Kmp_next (str,strlen(str), next); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Poj--2406power STRINGS+KMP to find the minimum period of a string