forked from Diusrex/UVA-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
10298 Power Strings.cpp
57 lines (45 loc) · 1.07 KB
/
10298 Power Strings.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
55
56
57
#include <iostream>
#include <string>
using namespace std;
// Use KMP to get the decrease used
int T[1000005];
void ComputeSpecialKMPTable(const string & n)
{
int size = n.size();
T[0] = -1;
T[1] = 0;
int pos = 2;
int cnd = 0;
while (pos < size)
{
if (n[pos - 1] == n[cnd])
{
cnd = cnd + 1;
T[pos] = cnd;
++pos;
}
else if (cnd > 0)
{
cnd = T[cnd];
}
else
{
T[pos] = 0;
++pos;
}
}
}
int main()
{
string n;
while (cin >> n, n.find('.') == string::npos)
{
ComputeSpecialKMPTable(n);
int startIndex = n.size() - 1;
int finalIndex = T[startIndex];
if (finalIndex != -1 && n[finalIndex] != n[startIndex])
finalIndex = min(0, finalIndex - 1); // Need to handle when it should actually be full length of string
int jump = startIndex - finalIndex;
cout << (n.size() / jump) << '\n';
}
}