-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathE.cpp
191 lines (164 loc) · 3.56 KB
/
E.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include<bits/stdc++.h>
using namespace std;
#define intll long long
#define endl '\n'
#define TEST_CASE false
template<typename T>
void PRINT(vector<T>& arr) {
for (int i = 0; i < int(arr.size()); i++)
cout << arr[i] << " \n"[i == int(arr.size()) - 1];
}
// merge y into x
// template<typename T = priority_queue<int>>
// void small_to_large(T& x, T& y) {
// if (x.size() <= y.size()) {
// while (!x.empty()) {
// y.push(x.top());
// x.pop();
// }
// x.swap(y);
// }
// else {
// while (!y.empty()) {
// x.push(y.top());
// y.pop();
// }
// }
// }
// merge y into x
template<typename T = priority_queue<int>>
void small_to_large(T& x, T& y) {
if (x.size() < y.size())
x.swap(y);
while (!y.empty()) {
x.push(y.top());
y.pop();
}
// y.clear();
}
struct vertex {
bool exist;
vector<int> child;
int freq;
vertex() {
exist = false;
freq = 0;
child.assign(26, -1);
}
};
struct Trie {
int root, next;
vector<vertex> TRIE;
Trie() : Trie(0) {}
Trie(int n) {
TRIE.resize(n + 1);
root = 0;
next = 1;
}
void ADD_VERTEX() {
TRIE.push_back(vertex());
}
void insert(string word) {
int cur = root;
int d = 1;
for (char c : word) {
int alphabet_num = tolower(c) - 'a'; // Convert to lowercase
if (TRIE[cur].child[alphabet_num] == -1) {
if (next >= int(TRIE.size())) { // Out of range add new vertex
ADD_VERTEX();
}
TRIE[cur].child[alphabet_num] = next++;
}
cur = TRIE[cur].child[alphabet_num];
TRIE[cur].freq = d; d++;
}
// TRIE[cur].freq++;
TRIE[cur].exist = true;
}
// Erase a string from trie
void erase(string word) {
int cur = root;
for (auto c : word) {
int alphabet_num = tolower(c) - 'a';
if (TRIE[cur].child[alphabet_num] == -1)
return;
cur = TRIE[cur].child[alphabet_num];
}
TRIE[cur].freq--;
if (TRIE[cur].freq == 0)
TRIE[cur].exist = false;
}
// Searches if word present in trie
// Freq for frequency count
bool search(string word) {
int cur = root;
for (auto c : word) {
int alphabet_num = tolower(c) - 'a';
if (TRIE[cur].child[alphabet_num] == -1)
return false;
cur = TRIE[cur].child[alphabet_num];
}
return TRIE[cur].exist;
}
// Return LCP sizes
// change int to long long
int LCP(string prefix) {
int cur = root;
int lcp_cnt = 0;
for (auto c : prefix) {
int alphabet_num = tolower(c) - 'a';
if (TRIE[cur].child[alphabet_num] == -1) {
break;
}
cur = TRIE[cur].child[alphabet_num];
lcp_cnt++;
// lcp_cnt += TRIE[cur].freq++;
}
return lcp_cnt;
}
};
Trie tr;
void dfs(int cur, priority_queue<int>& pq) {
for (int i = 0; i < 26; i++) {
if (tr.TRIE[cur].child[i] == -1)
continue;
priority_queue<int> qp;
dfs(tr.TRIE[cur].child[i], qp);
small_to_large(pq, qp);
}
if (!tr.TRIE[cur].exist and !pq.empty()) pq.pop();
pq.push(tr.TRIE[cur].freq);
};
void solution() {
int n;
cin >> n;
tr = Trie(0);
while (n--) {
string s;
cin >> s;
tr.insert(s);
}
intll ans = 0;
for (int i = 0; i < 26; i++) {
if (tr.TRIE[0].child[i] == -1)
continue;
priority_queue<int> pq;
dfs(tr.TRIE[0].child[i], pq);
while (!pq.empty()) {
ans += pq.top();
pq.pop();
}
}
cout << ans << endl;
}
int32_t main() {
ios::sync_with_stdio(false) ; cin.tie(0) ;
int t_c = 1, tt_c = 1;
if (TEST_CASE) cin >> t_c;
while (t_c--) {
// cout << "Case " << tt_c++ << ": ";
solution();
// if (t_c) cout << '\n';
}
return 0;
}