这两道题就是套一下模板:
poj1961&poj 2406
/×poj1961×/ #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; int next[1000010]; char s[1001000]; void getnext(){ int len = strlen(s); next[0]=0; for(int i=1;i<len;i++){ int k=next[i-1]; while(k!=0&&s[k]!=s[i]) k=next[k-1]; if(s[k]==s[i]) next[i]=k+1; else next[i]=0; } } int main (){ int n; int cas =0; while(scanf("%d",&n)==1){ if(n==0) break; cin>>s; getnext(); printf("Test case #%d\n",++cas); for(int i=2;i<=n;i++){ if(i%(i-next[i-1])==0){ int ans=i/(i-next[i-1]); if(ans>1){ printf("%d %d\n",i,ans); } } } puts(""); } }
/×2406×/ #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; int next[1000010]; char s[1001000]; void getnext(){ int len = strlen(s); next[0]=0; for(int i=1;i<len;i++){ int k=next[i-1]; while(k!=0&&s[k]!=s[i]) k=next[k-1]; if(s[k]==s[i]) next[i]=k+1; else next[i]=0; } } int main (){ int n; int cas =0; while(cin>>s){ if(s[0]=='.')break; int n=strlen(s); getnext(); int ans=n/(n-next[n-1]); printf("%d",(n%(n-next[n-1])==0)?ans:1); puts(""); } }
原文:http://blog.csdn.net/u013076044/article/details/42213235