POJ 2406 字符串a的n次方 kmp

来源:互联网 发布:wireshark抓端口 编辑:程序博客网 时间:2024/05/29 17:25

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


题意:找a的n次方,与s串匹配。


#include<stdio.h>#include<string.h>const int max=1000005;char str[max];int len,next[max],ans[max];void get_next(){    int i=0,j=-1;    next[0]=-1;    while(i<len){        if(j==-1||str[i]==str[j]){             i++;                                j++;                                if (str[i] != str[j])      /*这里有一个kmp的优化*/                next[i] = j;            else                next[i] = next[j];        }        else            j=next[j];                 }                               }main(){    while(scanf("%s",str)!=EOF&&strcmp(str,".")!=0){        len=strlen(str);        get_next();        if(len%(len-next[len])==0)        //if(next[len]>=len/2)            printf("%d\n",len/(len-next[len]));        else            printf("1\n");    }}