Power Strings

题目连接

 1 #include <stdio.h>
 2 #include <algorithm>
 3 #include <string.h>
 4 using namespace std;
 5 
 6 inline int read()
 7 {
 8     int x=0,f=1;char ch=getchar();
 9     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
10     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
11     return x*f;
12 }
13 
14 
15 /********************************************************************/
16 
17 const int maxn = 2e7+7;
18 char s[maxn];
19 int Next[maxn];
20 
21 
22 int main(){
23     while(~scanf("%s", s+1)){
24         if(s[1] == '.') break;
25         int len = strlen(s+1);
26         int k = 0;
27         Next[1] = 0;
28         for(int i = 2;i <= len;i++){
29             while(k > 0 && s[k+1] != s[i]){
30                 k = Next[k];
31             }
32             if(s[k+1] == s[i]){
33                 k++;
34             }
35             Next[i] = k;
36         }
37 
38         if(len%(len-Next[len])){
39             printf("1\n");
40         }
41         else printf("%d\n", len/(len-Next[len]));
42 
43     }
44     return 0;
45 }

 

posted @ 2018-10-01 20:38  ouyang_wsgwz  阅读(204)  评论(0编辑  收藏  举报