-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJohoon_No4354.cpp
More file actions
65 lines (49 loc) · 1.49 KB
/
Johoon_No4354.cpp
File metadata and controls
65 lines (49 loc) · 1.49 KB
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
58
59
60
61
62
63
64
65
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<string> T;
vector<int> FailureFunction(vector<string>& parent) {
int cases = parent.size();
vector<int> ans(cases, 0);
for (int tc = 0; tc < cases; tc++) {
int parentSize = parent[tc].size();
int j = 0;
vector<int> fail(parentSize, 0);
bool doesHaveValue = false;
for (int i = 1; i < parentSize; i++) {
while (j > 0 && parent[tc][i] != parent[tc][j]) {
j = fail[j - 1];
}
if (parent[tc][i] == parent[tc][j]) fail[i] = ++j;
if (i % 2) // if i is odd number
if (fail[i] == (i + 1) / 2) // if fail[i] is doubly repeated sentence
if (parentSize % ((i + 1) / 2) == 0) {
string patternTemp = parent[tc].substr(0, (i - 1) / 2 + 1);
int patternTempSize = patternTemp.size();
for (int j = 0; j < parentSize; j += patternTempSize) {
if (patternTemp != parent[tc].substr(j,patternTempSize)) break;
if (j == parentSize - patternTempSize) {
ans[tc] = parentSize / patternTempSize;
doesHaveValue = true;
}
}
}
// ****************************
if (ans[tc] || i == parentSize - 1) break;
}
if (!doesHaveValue) ans[tc] = 1;
}
return ans;
}
int main() {
while (1) {
string input;
getline(cin,input);
if (input == ".") break;
T.push_back(input);
}
vector<int> ans = FailureFunction(T);
for (int i = 0; i < ans.size(); i++)
cout << ans[i] << '\n';
}