forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0820.cpp
36 lines (33 loc) · 905 Bytes
/
0820.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
const int M = 15000;
int son[M][26], nodes[M], idx;
void insert(string& str, int id) {
int p = 0;
for (int i = str.size() - 1; ~i; i--) {
int& s = son[p][str[i] - 'a'];
if (!s) s = ++idx;
p = s;
}
nodes[p] = id;
}
class Solution {
public:
int minimumLengthEncoding(vector<string>& words) {
memset(son, 0, sizeof son);
memset(nodes, 0, sizeof nodes);
idx = 0;
for (int i = 0; i < words.size(); i++) insert(words[i], i + 1);
int res = 0;
for (int i = 1; i <= idx; i++) {
if (nodes[i] == 0) continue;
bool flag = false;
for (int j = 0; j < 26; j++) {
if (son[i][j]) {
flag = true;
break;
}
}
if (!flag) res += words[nodes[i] - 1].size() + 1;
}
return res;
}
};