poj 1961 (求字符串中的重复子串)

时间:2023-03-10 00:39:50
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

 #include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std; const int MAXN=;
char T[MAXN];
int next[MAXN];
int n;
void getNext()
{
int j,k;
j=;
k=-;
next[]=-;
while(j < n)
{
if(k==-||T[j]==T[k])
{
j++;
k++;
if(j%(j-k)==&&j/(j-k)>)
printf("%d %d\n",j,j/(j-k));
next[j]=k;
}
else k=next[k];
}
}
int main()
{ int iCase=;
while(scanf("%d",&n),n)
{
iCase++;
scanf("%s",&T);
printf("Test case #%d\n",iCase);
getNext();
printf("\n");
}
return ;
}