标签:
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 38908 | Accepted: 16170 |
Description
Input
Output
Sample Input
abcd aaaa ababab .
Sample Output
1 4 3
Hint
Source
#include <cstdio> using namespace std; const int N(1e6+5); char s[N]; int nt[N]; int main(){ for(int k, ls, p, ans; scanf("%s", s), *s!=‘.‘; printf("%d\n",ans)){ k=0; for(int i=ls=1; s[i]; i++, ls++){ for(;k&&s[k]!=s[i];k=nt[k-1]); nt[i]=s[k]==s[i]?++k:k; } p=ls-nt[ls-1]; ans=1; if(p&&ls%p==0){ ans=ls/p; for(int i=ls; i; i-=p) if(i-nt[i-1]!=p){ ans=1; break; } } } }
正解在此
#include <cstdio> using namespace std; const int N(1e6+5); char s[N]; int nt[N]; int main(){ for(int k, ls, p, ans; scanf("%s", s), *s!=‘.‘;){ k=0; for(int i=ls=1; s[i]; i++, ls++){ for(;k&&s[k]!=s[i];k=nt[k-1]); nt[i]=s[k]==s[i]?++k:k; } printf("%d\n", ls%(ls-nt[ls-1])?1:ls/(ls-nt[ls-1])); } }
标签:
原文地址:http://www.cnblogs.com/Patt/p/4921848.html