-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy paththore.cpp
54 lines (49 loc) · 1.14 KB
/
thore.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
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<string> vs;
struct node {
vector<node*> C;
int diff = INT_MIN;
node() : C(256, 0) {}
};
void build(node *p, const string &s, int d) {
for (size_t i = 0; i < s.size(); ++i) {
if (!p->C[s[i]]) p->C[s[i]] = new node();
p = p->C[s[i]];
p->diff = max(p->diff, d);
}
}
int solve(node *p, const string &s) {
int res = 0;
for (size_t i = 0; i < s.size(); ++i) {
p = p->C[s[i]];
#ifdef DEBUG
printf("%c diff(%d)\n", s[i], p->diff);
#endif
if (!p->diff && !res) res = i + 1;
}
return p->diff <= 0 ? res : 0;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
vs names(n);
int t = 0;
for (int i = 0; i < n; ++i) {
cin >> names[i];
if (names[i] == "ThoreHusfeldt") t = i;
}
if (t == 0) printf("Thore is awesome\n");
else {
node *root = new node();
for (int i = 0; i < n; ++i) build(root, names[i], t - i);
string prefix = "ThoreHusfeld";
int res = solve(root, prefix);
if (!res) printf("Thore sucks\n");
else printf("%s\n", prefix.substr(0, res).c_str());
}
return 0;
}