关于KMP的最短循环节、循环周期,请戳:

http://www.cnblogs.com/chenxiwenruo/p/3546457.html (KMP模板,最小循环节)

POJ 2406  Power Strings

题意:
给一个字符串,问这个字符串是否能由另一个字符串重复R次得到,求R的最大值。

#include <iostream>
#include <stdio.h>
#include <string.h>
/*
题意:
给一个字符串,问这个字符串是否能由另一个字符串重复R次得到,求R的最大值。
*/
using namespace std;
const int maxn=1000005;
int next[maxn];
char str[maxn];
void getnext(char *str,int len){
    next[0]=-1;
    int i=0,j=-1;
    while(i<len){
        if(j==-1||str[i]==str[j]){
            i++;
            j++;
            next[i]=j;
        }
        else
            j=next[j];
    }
}
int main()
{
    while(scanf("%s",str)!=EOF){
        if(str[0]=='.')
            break;
        int len=strlen(str);
        getnext(str,len);
        int l=len-next[len];
        if(len%l==0)
            printf("%d\n",len/l);
        else
            printf("1\n");
    }
    return 0;
}
View Code

相关文章: