poj-2406 Power Strings睡前一水~

来源:互联网 发布:人类曾经被毁灭 知乎 编辑:程序博客网 时间:2024/06/05 01:13

http://poj.org/problem?id=2406

睡前一水~ 一看就觉得是KMP的next数组...

然后果然是

#include <cstdio>#include <cstring>#include <iostream>#define max 1000010int n[max];char str1[max];int next(char s[]){    n[0]=-1;    int j=0,k=-1;    int len=strlen(s);    while(j<len)    {        if(k==-1||s[j]==s[k])            n[++j]=++k;        else k=n[k];    }    j=len-k;    if(len%j==0)        return len/j;    else        return 1;}int main(){    while(~scanf("%s",&str1))    {        if(str1[0]=='.')            break;        printf("%d\n", next(str1));    }    return 0;}