-
Notifications
You must be signed in to change notification settings - Fork 0
/
Password.cpp
64 lines (58 loc) · 1.54 KB
/
Password.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
58
59
60
61
62
63
64
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;
vector<long long> zAlgorithm(string &input) {
vector<long long> zArr (input.size(), 0);
int l = 0;
int r = 0;
for (int i = 1; i < input.size(); i++) {
if (i > r) {
l = i;
r = i;
while (r < input.size() && input[r] == input[r-l]) {
r++;
}
r--;
zArr[i] = r - l + 1;
} else {
int offset = i - l;
if (zArr[offset] < r - i + 1) {
zArr[i] = zArr[offset];
} else {
l = i;
while (r < input.size() && input[r] == input[r-l]) {
r++;
}
r--;
zArr[i] = r - l + 1;
}
}
}
return zArr;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
string s;
cin >> s;
vector<long long> zArr = zAlgorithm(s);
long long maxMiddle = 0;
long long count = 0;
for (int i = 0; i < zArr.size(); i++) {
if (i + zArr[i] == zArr.size()) {
if (maxMiddle >= zArr[i]) {
count = max(zArr[i], count);
}
maxMiddle = max(zArr[i]-1, maxMiddle);
} else {
maxMiddle = max(zArr[i], maxMiddle);
}
}
if (count == 0) {
cout << "Just a legend\n";
} else {
cout << s.substr(0, count) << '\n';
}
return 0;
}