poj1961 Period

不得不说下, 这题和2406 几乎是用同样的方法来做。

只要这题再枚举下每一个点就可以了。 判断是从第二个字符开始的。 还要判断该字符的下一个next[]  不是0 就可以了

View Code
#include<stdio.h>
#include<string.h>

#define maxn 1000008

int next[maxn];
char sift[maxn];
int len;

void get_next()
{
    int i = 0, j = -1;
    next[0] = -1;

    while (i <= len)
    {
        if (j == -1 || sift[j] == sift[i])
        {
            i ++;
            j ++;
            next[i] = j;
        }
        else
        {
            j = next[j];
        }
    }
    for (i = 1; i < len; i ++)
    {
        if (next[i + 1] != 0 && (i + 1) % (i + 1 - next[i + 1]) == 0)
        {
            printf("%d %d\n", i + 1, (i + 1) / (i + 1 - next[i + 1]));
        }
    }
    printf("\n");
    return;
}

int main()
{
    int count = 0;

    while (scanf("%d", &len) != EOF)
    {
        if (len == 0)
        {
            break;
        }
        getchar();
        gets(sift);
        printf("Test case #%d\n", ++count);
        get_next();
    }
    return 0;
}
原文地址:https://www.cnblogs.com/cxiaoln/p/2441654.html