Cycle (cycle seeking each prefix string section

# Meaning of the questions
given a string, the string each prefix number to obtain the shortest cycle that appears in each section prefix

# Explanations

Next the entire string array is determined, for each prefix, i-next [i] is the smallest section of the length of the cyclic prefix,

If i is divisible, the prefix i loop section constituted by a complete cycle, the number can be determined

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int N=1e6+10;
 4 int tt;
 5 int n;
 6 char s[N];
 7 int main(){
 8    while(scanf("%d%s",&n,s+1)&&n){
 9       printf("Test case #%d\n",++tt);
10       int ne[N];
11       memset(ne,0,sizeof ne);
12       for(int i = 2,j=0; i <= n; i++){
13          while(j && s[i] != s[j+1])
14             j = ne[j];
15          if(s[i] == s[j+1])
16             j++;
17          ne[i] = j;
18       }
19       for(int i=2;i<=n;i++){
20          if( i % (i - ne[i]) == 0 && i / (i - ne[i]) > 1)
21             printf("%d %d\n",i,i/(i-ne[i]));
22       }
23       puts("");
24    }
25 }

 

Guess you like

Origin www.cnblogs.com/hhyx/p/12521546.html