传送门:点我
Sample Input
3 aaa 12 aabaabaabaab 0
Sample Output
Test case #1 2 2 3 3 Test case #2 2 2 6 2 9 3 12 4题意:给一个字符串判断前缀是否构成周期串,输出周期串长度和周期数
知识点:如果一个字符串有最小周期,那么循环节为n-next[n],周期数为n / (n-next[n])
证明:http://blog.csdn.net/zz_ylolita/article/details/50650394
#include<iostream>
#include<cstdio>
#include<cstring>
#define N 1000005
using namespace std;
char s[N];
int Next[N];
void getnext(int n)
{
int i,j,len;
for(j=0,i=1; i<n; i++)
{
while(j>0&&s[i]!=s[j])
j=Next[j-1];
if(s[i]==s[j])
j++;
Next[i]=j;
if(Next[i]!=0)
{
len=i+1;
if(len%(len-Next[i])==0)
printf("%d %d\n",len,len/(len-Next[i]));
}
}
}
int main()
{
int t,cat=0;
while(scanf("%d",&t),t)
{
Next[0]=0;
scanf("%s",s);
int len=strlen(s);
printf("Test case #%d\n",++cat);
getnext(len);
cout<<endl;
}
}