题目描述

Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of concatenation as multiplication, exponentiation by a non-negative integer is defined in the normal way: a^0 = "" (the empty string) and a^(n+1) = a*(a^n).

输入

Each test case is a line of input representing s, a string of printable characters. The length of s will be at least 1 and will not exceed 1 million characters. A line containing a period follows the last test case.

输出

For each s you should print the largest n such that s = a^n for some string a.

样例输入

abcd
aaaa
ababab
.

样例输出

1
4
3
 1 #define UnQuestion4
 2 #ifdef Question4
 3 
 4 #define UnDebug
 5 
 6 #ifdef Debug
 7 #include <ctime>
 8 #include <cstdio>
 9 #endif
10 
11 #include <iostream>
12 #include <cstring>
13 
14 using namespace std;
15 
16 const int Max = 1e6;
17 int Next[Max + 10];
18 
19 void makeNext(char str[], int len);
20 
21 int main()
22 {
23 #ifdef Debug
24     freopen("in.txt", "r", stdin);
25     clock_t start, finish;
26     double totaltime;
27     start = clock();
28 #endif
29 
30     char str[Max + 10];
31     while (cin >> str && str[0] != '.')
32     {
33         int len = strlen(str);
34         makeNext(str, len);
35         if (len % (len - Next[len]) == 0)
36             cout << len / (len - Next[len]) << endl;
37         else
38             cout << 1 << endl;
39     }
40 
41 #ifdef Debug
42     finish = clock();
43     totaltime = (double)(finish - start) / CLOCKS_PER_SEC;
44     cout << "Time : " << totaltime * 1000 << "ms" << endl;
45     system("pause");
46 #endif
47     return 0;
48 }
49 
50 void makeNext(char str[], int len)
51 {
52     int i = 0, j = -1;
53     memset(Next, -1, sizeof(Next));
54     Next[0] = -1;
55     while (i < len)
56     {
57         if (j == -1 || str[i] == str[j])
58         {
59             j++;
60             i++;
61             Next[i] = j;
62         }
63         else
64             j = Next[j];
65     }
66 }
67 #endif
View Code

 

 

 
posted on 2019-05-23 17:12  jephsdge  阅读(182)  评论(3)    收藏  举报