您的位置:首页 > 其它

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

2015-05-12 16:58 162 查看

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=1000010;
char T[MAXN];
int next[MAXN];
int n;
void getNext()
{
int j,k;
j=0;
k=-1;
next[0]=-1;
while(j < n)
{
if(k==-1||T[j]==T[k])
{
j++;
k++;
if(j%(j-k)==0&&j/(j-k)>1)
printf("%d %d\n",j,j/(j-k));
next[j]=k;
}
else k=next[k];
}
}
int main()
{

int iCase=0;
while(scanf("%d",&n),n)
{
iCase++;
scanf("%s",&T);
printf("Test case #%d\n",iCase);
getNext();
printf("\n");
}
return 0;
}
View Code

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: