Topic link: https: //vjudge.net/problem/POJ-1961
The meaning of problems: Given a string of length n, (n <= 1e6), for index i (2 <= i <= n), if the substring s (1 ... i) is a periodic sub-string output The maximum period.
Ideas:
Investigation of the algorithm defined kmp next master array, if the (i + 1)% (ij) == 0 && (i + 1) / (ij)> 1, then the condition is met when the substring.
AC Code:
#include<cstdio> #include<algorithm> #include<cstring> using namespace std; const int maxn=1e6+5; int n,cas,nex[maxn]; char s[maxn]; void get_next(){ int j; j=nex[0]=-1; for(int i=1;i<n;++i){ while(j>-1&&s[i]!=s[j+1]) j=nex[j]; if(s[i]==s[j+1]) ++j; nex[i]=j; if((i+1)%(i-j)==0&&(i+1)/(i-j)>1) printf("%d %d\n",i+1,(i+1)/(i-j)); } } int main(){ while(scanf("%d",&n),n){ scanf("%s",s); printf("Test case #%d\n",++cas); get_next(); printf("\n"); } return 0; }