-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP10298.cpp
54 lines (51 loc) · 1.16 KB
/
P10298.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <stdio.h>
#include <iostream>
int main() {
char *w = new char[1000009];
bool *possiblePeriods = new bool[1000001];
while(true) {
gets(w);
// Find length:
int len = 0;
while(isprint(w[len]))
++len;
if(len == 1 && w[0] == '.')
break;
if(len == 0) {
std::cout << "0" << std::endl;
continue;
}
w[len] = '\0';
//std::cerr << "len=" << len << ", w=" << w << std::endl;
//memset(possiblePeriods, true, len+1);
for(int i = 1; i < len; ++i) {
possiblePeriods[i] = len%i == 0;
}
// Find period:
int best = 1;
for(int period = len-1; period >= 1; --period) {
if(!possiblePeriods[period])
continue;
bool ok = true;
for(int i = period; i < len; ++i) {
if(w[i] != w[i-period]) {
ok = false;
break;
}
}
if(ok) {
//std::cerr << "Found best period=" << period << std::endl;
best = len/period;
}
else {
for(int smaller = 2; smaller < period; ++smaller)
if(period % smaller == 0)
possiblePeriods[smaller] = false;
}
}
std::cout << best << std::endl;
}
delete[] possiblePeriods;
delete[] w;
return 0;
}