poj1961—Period(kmp周期性质)

传送门:点我

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;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值