Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1358
Explanation of output data,
Group 1:
2 2 indicates the first letter to the second letter, and a appears twice.
3 3 indicates the first letter to the third letter, and a appears three times.
Group 2:
2 2 indicates the first letter to the second letter, and a appears twice. 6 2 indicates the first letter to the sixth letter, and AAB appears twice. 9 3 indicates the first letter to the ninth letter, and AAB appears three times. 12 4 indicates the first letter to 12th letters, and AAB appears four times.
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 5 using namespace std; 6 7 int next[1000010]; 8 char str[1000010]; 9 10 void get_next()11 {12 int i=0,j=-1;13 next[0]=-1;14 int lens=strlen(str);15 while (i<lens)16 {17 if (j==-1||str[i]==str[j])18 {19 i++;20 j++;21 next[i]=j;22 if (i>=2&&j>=1)23 {24 if (i%(i-next[i])==0)25 {26 int k=i/(i-next[i]);27 printf ("%d %d\n",i,k);28 }29 }30 }31 else32 j=next[j];33 }34 }35 int main ()36 {37 int n,flag=1;38 while (scanf("%d",&n),n)39 {40 getchar();41 scanf("%s",str);42 printf ("Test case #%d\n",flag++);43 get_next();44 printf ("\n");45 //lens=strlen(str);46 }47 return 0;48 }