-
Notifications
You must be signed in to change notification settings - Fork 111
/
delete-duplicate-folders-in-system.cc
73 lines (70 loc) · 2.04 KB
/
delete-duplicate-folders-in-system.cc
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
// Delete Duplicate Folders in System
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
class Solution {
public:
static const long P = 1000000007;
long inv(long x) {
long s = 1;
for (; x > 1; x = P%x)
s = s*(P-P/x)%P;
return s;
}
vector<vector<string>> deleteDuplicateFolder(vector<vector<string>>& paths) {
sort(ALL(paths));
unordered_map<string, int> code;
unordered_map<int, string> name;
vector<int> a, cur;
vector<pair<int, int>> ranges;
for (auto &path: paths) {
auto res = code.try_emplace(path.back(), code.size()+1);
int tail = res.first->second;
if (res.second)
name.try_emplace(tail, path.back());
for (; cur.size() && (cur.size() > path.size() || cur.size() == path.size() && a[cur.back()] != tail); cur.pop_back()) {
a.push_back(0);
ranges.emplace_back(cur.back(), a.size());
}
cur.emplace_back(a.size());
a.push_back(tail);
}
for (; cur.size(); cur.pop_back()) {
a.push_back(0);
ranges.emplace_back(cur.back(), a.size());
}
long k = code.size()+1, pow = 1;
vector<long> h(a.size()+1), pw(a.size());
REP(i, a.size()) {
pw[i] = pow;
h[i+1] = (h[i]+pow*a[i]) % P;
pow = pow*k % P;
}
unordered_map<long, pair<int, int>> seen;
cur.assign(a.size()+1, 0);
for (auto [l, r]: ranges) {
if (r-l == 2) continue;
long v = (h[r-1]-h[l+1]+P)*inv(pw[l+1]) % P;
auto res = seen.try_emplace(v, pair<int, int>{l, r});
if (!res.second) {
cur[l]++;
cur[r]--;
cur[res.first->second.first]++;
cur[res.first->second.second]--;
}
}
int acc = 0;
vector<string> path;
vector<vector<string>> ans;
REP(i, a.size()) {
acc += cur[i];
if (a[i]) {
path.push_back(name[a[i]]);
if (!acc)
ans.push_back(path);
} else
path.pop_back();
}
return ans;
}
};