-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcountoccurencesofstringtrie.cpp
102 lines (86 loc) · 2.03 KB
/
countoccurencesofstringtrie.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
Problem : https : // codeforces.com/contest/858/problem/D
/*
______ _ _ ____ _ _ _ _ _ _______ _
| ____| (_) | _ \ | | | | | \ | | | |__ __(_)
| |__ __ _ _| | |_) | ___| |_| |_ ___ _ __| \| | _____ _| |_ | | _ _ __ ___
___
| __/ _` | | | _ < / _ \ __| __/ _ \ '__| . ` |/ _ \ \/ / __|| | | | '_ ` _
\ / _ \ | | | (_| | | | |_) | __/ |_| || __/ | | |\ | __/> <| |_ | | | |
| | | | | __/
|_| \__,_|_|_|____/ \___|\__|\__\___|_| |_| \_|\___/_/\_\\__||_| |_|_| |_|
|_|\___|
*/
#include <bits/stdc++.h>
#define int long long
#define sz(x) (int)(x.size())
using namespace std;
class node {
public:
node *child[10];
int cnt;
node() {
for (int i = 0; i < 10; i++)
child[i] = NULL;
cnt = 0;
}
};
typedef node *nodeptr;
class TRIE {
private:
nodeptr root;
public:
TRIE() { root = new node(); }
void insert(string &s) {
int n = sz(s);
nodeptr curr = root;
for (int i = 0; i < n; i++) {
int next = s[i] - '0';
if (curr->child[next] == NULL)
curr->child[next] = new node();
curr = curr->child[next];
}
curr->cnt++;
}
int getCount(string &s) {
nodeptr curr = root;
for (int i = 0; i < sz(s); i++) {
int next = s[i] - '0';
if (curr->child[next] == NULL)
return 0;
curr = curr->child[next];
}
return curr->cnt;
}
};
set<string> f[70000];
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
TRIE T;
for (int i = 0; i < n; i++) {
string s;
cin >> s;
for (int j = 0; j < sz(s); j++) {
string curr;
for (int k = j; k < sz(s); k++) {
curr += s[k];
f[i].insert(curr);
}
}
for (string it : f[i])
T.insert(it);
}
for (int i = 0; i < n; i++) {
string ans = "99999999999";
for (string it : f[i]) {
int cnt = T.getCount(it);
if (cnt == 1 && (sz(ans) > sz(it)))
ans = it;
}
cout << ans << '\n';
}
return 0;
}