SDUTOJ 2475 Power Strings
阿新 • • 發佈:2017-07-19
mod 循環節 file 算法 技術分享 ++i size _id 循環
<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; }
SDUTOJ 2475 Power Strings