-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPassword.cpp
More file actions
88 lines (78 loc) · 1.69 KB
/
Copy pathPassword.cpp
File metadata and controls
88 lines (78 loc) · 1.69 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
Problem: https://codeforces.com/problemset/problem/126/B
Tags: Strings, KMP
Status: Accepted
*/
#include <bits/stdc++.h>
using namespace std;
#define debugx(x) cerr << #x << ": " << x << endl
#define rep(i, n) for (int i = 0; i < n; ++i)
#define repx(i, x, n) for (int i = x; i < n; ++i)
#define pb push_back
#define eb emplace_back
typedef long long ll;
typedef vector<int> vi;
typedef pair<ll, ll> par;
typedef vector<par> vp;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<vi> graph;
typedef vector<vector<par>> wgraph;
typedef vector<par> vpar;
vector<int> prefix(string &S)
{
vector<int> p(S.size());
p[0] = 0;
for (int i = 1; i < S.size(); ++i)
{
p[i] = p[i - 1];
while (p[i] > 0 && S[p[i]] != S[i])
p[i] = p[p[i] - 1];
if (S[p[i]] == S[i])
p[i]++;
}
return p;
}
int KMP(string &P, string &S, vi &pi)
{
int n = S.length(), m = P.length();
int j = 0, ans = 0;
for (int i = 0; i < n; ++i)
{
while (j > 0 && S[i] != P[j])
j = pi[j - 1];
if (S[i] == P[j])
++j;
if (j == P.length())
{
ans++;
j = pi[j - 1];
}
}
return ans;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
string input;
cin >> input;
vi p = prefix(input);
string x = input.substr(0, p[p.size() - 1]);
while (x.size() > 0)
{
if (KMP(x, input, p) >= 3)
{
cout << x << "\n";
return 0;
}
else
{
p = prefix(x);
x = input.substr(0, p[p.size() - 1]);
}
}
cout << "Just a legend\n";
return 0;
}