poj 2406 Power Strings

类似于1961题,不过比1961要简单,只是让求最长的前缀,要注意它要求输入"."时表示输入结束。

代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int next[1000005];
char str[1000005];

void init()
{
    int i,j;

    i=0;j=-1;
    next[0]=-1;
    while(str[i]!='\0')
    {
        if(j == -1 || str[i] == str[j])
        {
            i++;j++;
            next[i] = j;
        }
        else
        j = next[j];
    }
}

int main()
{
    //int i,maxx;

    while(scanf("%s",str) != EOF)
    {
        getchar();
        int len = strlen(str);
        if(len==1 && str[0]=='.')
        break;
        init();
        if(len%(len-next[len]) == 0 )
        printf("%d\n",len/(len-next[len]));
        else
        printf("1\n");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/misty1/p/2468861.html