您的位置:首页 > 其它

HDU 3746 Cyclic Nacklace(KMP)

2015-03-10 20:24 162 查看
KMP求最短循环节的应用

//2100 KB	218 ms	
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

#define M 100000+1000
char str[M];
int next2[M];
int len;
void getnext()
{
    len=strlen(str);
    int i=0,j;
    j=next2[0]=-1;
    while(i<len)
    {
        while(j!=-1&&str[i]!=str[j]) j=next2[j];
        j++;
        i++;
        next2[i]=j;
    }
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%s",str);
        getnext();
        int ans=(len-next2[len])-len%(len-next2[len]);
        if(len%(len-next2[len])==0&&len/(len-next2[len])>1) printf("0\n");
        else printf("%d\n",ans);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: