hdu1358_VJkuangbin16kmp_Period

题目大意:给定字符串的长度m和字符串,问对于前i(2<=i<=m)个字符串,如果该字符串的长度为该字符串的循环节长度的倍数,则输出i和该倍数

循环节长度:cc = i - next[i];

#include <cstdio>

using namespace std;
const int maxn = 1000010;
int next[maxn];
char p[maxn];
int m;

void getnext()
{
    int i = 0, j = -1;
    next[0] = -1;
    while(i < m)
    {
        if(j==-1 || p[i]==p[j])
        {
            ++i;
            ++j;
            next[i] = j;
        }
        else j = next[j];
    }
}


int main()
{
    int cas = 0;
    while(~scanf("%d", &m) && m)
    {
        printf("Test case #%d\n", ++cas);
        scanf("%s", p);
        getnext();
        //for(int i = 1; i <= m; ++i) printf("%d ", next[i]);
        for(int i = 2; i <= m; ++i)
        {
            int cc = i - next[i];
            if(cc == i) continue;
            if(i%cc == 0) printf("%d %d\n", i, i/cc);
        }
        printf("\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/jay__bryant/article/details/80729067