<pre class="cpp" name="code">#include<iostream> #include<stdio.h> #include<string.h> #define N 1000005 int next[N]; char s[N]; using namespace std; void getnext(char s[])//KMP算法的应用 { int j=-1,i=0,len; next[0]=-1; len=strlen(s); while(i<=len) { if(j==-1||s[i]==s[j]) { ++i; ++j; next[i]=j; } else j=next[j]; } } int main() { int len; while(cin>>s) { if(s[0]=='.') { break; } len=strlen(s); getnext(s); if(len%(len-next[len])==0) cout<<(len/(len-next[len]))<<endl;//求最小循环节..其实题意就是这个意思 } return 0; }
时间: 2024-10-13 00:16:05