poj 2406 Power Strings(kmp)

来源:互联网 发布:eclipse图形界面编程 编辑:程序博客网 时间:2024/06/08 23:59

注意判断剩下的一节是否能够被n整除

#include <stdio.h>#include <iostream>#include <string.h>#include <algorithm>using namespace std;const int maxn=1000010;int next[maxn],n;char s[maxn];void get_next(){   int j=0,k=-1;   next[0]=-1;   while(j<=n)   {       if(k==-1||s[j]==s[k])       {           j++;k++;           next[j]=k;       }       else k=next[k];   }}int main() {while(~scanf("%s",s)){    if(s[0]=='.')break;     n=strlen(s);    get_next();    if(n%(n-next[n])==0)//判断是否能被n整除    printf("%d\n",n/(n-next[n]));    else printf("1\n");}     return 0;}