hdu 3746 Cyclic Nacklace

有关最小循环节的解释

http://www.cnblogs.com/jackge/archive/2013/01/05/2846006.html

#include<stdio.h>
#include<string.h>
char str[100005];
int next[100005];
int len;

void getNext()
{
    next[0] = -1;
    int i = 0,j = -1;
    while(i < len)
    {
        if(j == -1 || str[i] == str[j])
            next[++i] = ++j;
        else j = next[j];
    }
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%s",str);
        len = strlen(str);
        getNext();
        int tmp = len - next[len];
        if(len % tmp == 0 && len != tmp)
            printf("0\n");
        else printf("%d\n",tmp - len%tmp);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/feynmanz/article/details/80303372
今日推荐