Power Strings
题目描述
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 #include<bits/stdc++.h>
2 using namespace std;
3 #define MAXN 1000017
4 int Next[MAXN];
5 int len;
6 void getNext(
char T[])
7 {
8 int i =
0, j = -
1;
9 Next[
0] = -
1;
10 while(i <
len)
11 {
12 if(j == -
1 || T[i] ==
T[j])
13 {
14 i++,j++
;
15 Next[i] =
j;
16 }
17 else
18 j =
Next[j];
19 }
20 }
21
22 int main()
23 {
24 char ss[MAXN];
25 int length;
26 while(cin >>
ss)
27 {
28 if(ss[
0] ==
'.')
29 break;
30 len =
strlen(ss);
31 getNext(ss);
32 length = len -
Next[len];
33 if(len%length ==
0)
34 cout << len/length <<
endl;
35 else
36 cout <<
"1" <<
endl;
37 }
38 return 0;
39 }
View Code
转载于:https://www.cnblogs.com/qing123tian/p/11107478.html
相关资源:数据结构—成绩单生成器