-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassword.cpp
59 lines (48 loc) · 1.11 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
#include <iostream>
#include <vector>
using namespace std;
template <typename T>
void print_vec(vector<T> v) {
for (int i=0; i<v.size(); i++) {
cout << v[i] << " ";
}
cout << endl;
}
vector<int> compute_prefix_kmp(string a) {
vector<int> pi(a.length());
pi[0] = 0;
for (int i=1; i < a.length(); i++) {
int j = pi[i-1];
while (j>0 && a[i]!=a[j]) {
j = pi[j-1];
}
if (a[i] == a[j]) {
j++;
}
pi[i] = j;
}
return pi;
}
int main() {
string input;
cin >> input;
vector<int> pi = compute_prefix_kmp(input);
// print_vec(pi);
// If the last element of the array is 0, it means we don't have a common prefix/suffix of the whole string
if (pi[input.length()-1] == 0) {
cout << "Just a legend" << endl;
return 0;
}
// Initialize max to the longest prefix length and found to the character before that
int max = pi[input.length()-1];
int found = pi[max-1];
// Search for the prefix/suffix in the string
for (int i=1; i < input.length()-1; i++) {
if (pi[i]==max) {
// cout << "qui " << max << endl;
found = max;
}
}
// Found now has the index+1 of the prefix/suffix
return 0;
}