Period

题目大意:有一个长N的字符串,如果前缀Ni是一个完全循环的串(循环次数大于1),输出Ni和它循环了多少次。

 
分析:输入next的应用,求出来next数组直接判断Ni是否是完全的循环就行了,也就是Ni % next[i] == 0
下面代码
=======================================================================================================================
#include<stdio.h>
#include<string.h>

const int MAXN = 1e6+7;
const int oo = 1e9+7;

char s[MAXN];
int next[MAXN];

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

    while(i < N)
    {
        if(j==-1 || s[i]==s[j])
            next[++i] = ++j;
        else
            j = next[j];
    }
}

int main()
{
    int N, t=1;

    while(scanf("%d", &N), N)
    {
        int i;

        scanf("%s", s);

        GetNext(N);

        printf("Test case #%d
", t++);
        for(i=2; i<=N; i++)
        {
            int k = i-next[i];
            if(next[i] && i % k == 0)
                printf("%d %d
", i, i/k);
        }
        printf("
");
    }

    return 0;
}
原文地址:https://www.cnblogs.com/liuxin13/p/4730785.html