链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=905
_rz,开始是暴力+KMP硬生生的超时了。
后来根据预处理数组的特性,终于过了--|||
2836474 | 2012-04-03 14:16:32 | Accepted | 1905 | C++ | 110 | 5064 | zisu_123 |
比如
s:a ba bab
P: 0 0 1 2 3 4
做法是len/(len-P[len]).
想想看预处理数组P的由来,上例中是红色部分是共同的,即是6-4=2.然后6/2=3.
#include<stdio.h> #include<string.h> char s[1000005]; int P[1000005]; int n; void initP() { P[0]=0; P[1]=0; int i,j=0,len=strlen(s); for(i=2;i<=len;i++) { while(j>0&&s[j]!=s[i-1]) j=P[j]; if(s[j]==s[i-1]) j++; P[i]=j; } } int main() { int len; while(~scanf("%s",s)) { if(s[0]=='.') break; initP(); len=strlen(s); printf("%d\n",len/(len-P[len])); } return 0; }
cqlf | 2406 | Accepted | 5288K | 141MS | G++ | 483B | 2012-04-03 14:34:24 |
#include<stdio.h> #include<string.h> char s[1000005]; int P[1000005]; int n; void initP() { P[0]=0; P[1]=0; int i,j=0,len=strlen(s); for(i=2;i<=len;i++) { while(j>0&&s[j]!=s[i-1]) j=P[j]; if(s[j]==s[i-1]) j++; P[i]=j; } } int main() { int len; while(~scanf("%s",s)) { if(s[0]=='.') break; initP(); len=strlen(s); if(len%(len-P[len])!=0) printf("1\n"); else printf("%d\n",len/(len-P[len])); } return 0; }
#include<stdio.h> #include<string.h> char temp[1005000]={0}; int main() { int i=0,N=0,ans=0,j=0; while(scanf("%s",&temp)) { N=strlen(temp); if(N==1&&temp[0]=='.') break; for(i=1;i<=N;i++) { if(N%i==0) { for(j=i;j<N;j+=i) { if(strncmp(temp,temp+j,i)) { break; } } if(j==N) { ans=N/i; break; } } } printf("%d\n",ans); } }