| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
KMP 贴个代码#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char s[1000005];
int next[1000005];
int n;
void getnext()
{
int j=0,k=-1;
next[0]=-1;
while(j<n)
{
if(k==-1 || s[j]==s[k])//求距离不修正。
{
j++;k++;
next[j]=k;
}
else k=next[k];
}
}
int main()
{
int i,t=1;
while(scanf("%d",&n) && n)
{
scanf("%s",s);
getnext();
cout<<"Test case #"<<t++<<endl;
for(i=2;i<=n;i++)
{
int t=i%(i-next[i]);
int k=i/(i-next[i]);
if(t==0 && k>1)
cout<<i<<' '<<k<<endl;
}
cout<<endl;
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator