| ||||||||||
| 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贴个代码
//题意:就是问一个字符串写成(a)^n的形式,求最大的n.
//已知字符串长度为l,对应的next[l],则回溯区间长d=k-next[k],如果k%d==0,那么t[1……k]最多可均匀的分成k/d份,
//也就是可以生成一个长度为d的重复度为k/d的字串。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char s[1000005];
int next[1000005];
int l;
void getnext()
{
int j=0,k=-1;
next[0]=-1;
while(j<l)
{
if(k==-1 || s[j]==s[k])
{
j++;k++;
if(s[j]!=s[k])
next[j]=k;
else
next[j]=next[k];
}
else k=next[k];
}
}
int main()
{
int ans;
while(scanf("%s",s) && strcmp(s,"."))
{
ans=1;
l=strlen(s);
getnext();
if(l%(l-next[l])==0)
ans=l/(l-next[l]);
cout<<ans<<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