poj 1961 (求字符串中的重复子串)
Sample Input
3
aaa
12
aabaabaabaab
0
Sample Output
Test case #1
2 2
3 3
Test case #2
2 2 //aa有2个a
6 2 //aabaab有2个aab
9 3
12 4
0 1 2 3 4 5 6 7 8 9 10 11
a a b a a b a a b a a b 的next数组为
-1 0 1 0 1 2 3 4 5 6 7 8 9
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<stdio.h> 2 #include<iostream> 3 #include<string.h> 4 #include<algorithm> 5 using namespace std; 6 7 const int MAXN=1000010; 8 char T[MAXN]; 9 int next[MAXN]; 10 int n; 11 void getNext() 12 { 13 int j,k; 14 j=0; 15 k=-1; 16 next[0]=-1; 17 while(j < n) 18 { 19 if(k==-1||T[j]==T[k]) 20 { 21 j++; 22 k++; 23 if(j%(j-k)==0&&j/(j-k)>1) 24 printf("%d %d\n",j,j/(j-k)); 25 next[j]=k; 26 } 27 else k=next[k]; 28 } 29 } 30 int main() 31 { 32 33 int iCase=0; 34 while(scanf("%d",&n),n) 35 { 36 iCase++; 37 scanf("%s",&T); 38 printf("Test case #%d\n",iCase); 39 getNext(); 40 printf("\n"); 41 } 42 return 0; 43 }