您的位置:首页 > 其它

poj 2406 求字符串中重复子串的个数

2015-05-12 17:00 295 查看

Sample Input

abcd
aaaa
ababab
.
Sample Output

1 //1个abcd
4 //4个a
3 //3个ab

 

 

#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++;

next[j]=k;
}
else k=next[k];

}
if(n%(n-k)==0)
printf("%d\n",n/(n-k));
else
printf("1\n") ;
}
int main()
{

while(scanf("%s",&T) != EOF)
{
if (T[0]=='.')
break ;
n = strlen(T) ;
getNext();

}
return 0;
}
View Code

 

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