poj 2406 Power Strings(KMP)

题目链接:http://poj.org/problem?

id=2406

题目大意:找出字串最大循环次数

方法:和上一个一样   传送门

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;
int next[1000010];
char str[1000010];

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

int main()
{
    int n;
    int cas = 0;
    while(~scanf("%s",str) && str[0]!='.')
    {
        int len = strlen(str);
        getnext(str,len);
        if(next[len] && len%(len-next[len])==0)
            printf("%d
",len/(len-next[len]));
        else
            printf("1
");

    }
    return 0;
}


原文地址:https://www.cnblogs.com/mfmdaoyou/p/7106540.html