forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
longest-common-subpath.cpp
147 lines (137 loc) · 4.91 KB
/
longest-common-subpath.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
// Time: O(m * nlogn)
// Space: O(n)
class Solution {
public:
int longestCommonSubpath(int n, vector<vector<int>>& paths) {
int left = 1, right = size(*min_element(cbegin(paths), cend(paths),
[](const auto& x, const auto& y) {
return size(x) < size(y);
}));
while (left <= right) {
const auto& mid = left + (right - left) / 2;
if (!check(paths, mid)) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return right;
}
private:
template<typename T>
struct VectorHash {
size_t operator()(const std::vector<T>& v) const {
size_t seed = 0;
for (const auto& i : v) {
seed ^= std::hash<T>{}(i) + 0x9e3779b9 + (seed<<6) + (seed>>2);
}
return seed;
}
};
bool check(const vector<vector<int>>& paths, int x) {
unordered_set<vector<int>, VectorHash<int>> intersect = RabinKarp(paths[0], x);
for (int i = 1; i < size(paths); ++i) {
intersect = set_intersection<vector<int>, VectorHash<int>>(intersect, RabinKarp(paths[i], x));
if (empty(intersect)) {
return false;
}
}
return true;
}
unordered_set<vector<int>, VectorHash<int>> RabinKarp(const vector<int>& arr, int x) {
static const int MOD = 1e9 + 7; // MOD could be the min prime of 7-digit number (1e6+3)
static const vector<int> P = {113, 109}; // double hashing, P could be {2, 3}
vector<int> hashes, powers;
for (const auto& p : P) {
int64_t h = 0, power = 1;
for (int i = 0; i < x; ++i) {
h = (h * p + arr[i]) % MOD;
power = (power * p) % MOD;
}
hashes.emplace_back(h);
powers.emplace_back(power);
}
unordered_set<vector<int>, VectorHash<int>> lookup = {hashes};
for (int i = x; i < size(arr); ++i) {
for (int j = 0; j < size(P); ++j) {
hashes[j] = ((int64_t(hashes[j]) * P[j] - int64_t(arr[i - x]) * powers[j] + arr[i]) % MOD + MOD) % MOD;
}
lookup.emplace(hashes);
}
return lookup;
}
template<typename T, typename H>
unordered_set<T, H> set_intersection(const unordered_set<T, H>& a,
const unordered_set<T, H>& b) {
if (a.size() > b.size()) {
return set_intersection(b, a);
}
unordered_set<T, H> result;
for (const auto& x : a) {
if (b.count(x)) {
result.emplace(x);
}
}
return result;
}
};
// Time: O(m * nlogn)
// Space: O(n)
class Solution2 {
public:
int longestCommonSubpath(int n, vector<vector<int>>& paths) {
int left = 1, right = size(*min_element(cbegin(paths), cend(paths),
[](const auto& x, const auto& y) {
return size(x) < size(y);
}));
while (left <= right) {
const auto& mid = left + (right - left) / 2;
if (!check(paths, mid)) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return right;
}
private:
bool check(const vector<vector<int>>& paths, int x) {
unordered_set<int64_t> intersect = RabinKarp(paths[0], x);
for (int i = 1; i < size(paths); ++i) {
intersect = set_intersection<int64_t>(intersect, RabinKarp(paths[i], x));
if (empty(intersect)) {
return false;
}
}
return true;
}
unordered_set<int64_t> RabinKarp(const vector<int>& arr, int x) {
static const int64_t MOD = 1e11 + 19; // MOD is the min prime of 12-digit number
static const int64_t P = 1e5 + 1; // max(x for p in paths for x in p)+1
int64_t h = 0, power = 1;
for (int i = 0; i < x; ++i) {
h = (h * P + arr[i]) % MOD;
power = (power * P) % MOD;
}
unordered_set<int64_t> lookup = {h};
for (int i = x; i < size(arr); ++i) {
h = ((h * P - arr[i - x] * power + arr[i]) % MOD + MOD) % MOD;
lookup.emplace(h);
}
return lookup;
}
template<typename T>
unordered_set<T> set_intersection(const unordered_set<T>& a,
const unordered_set<T>& b) {
if (a.size() > b.size()) {
return set_intersection(b, a);
}
unordered_set<T> result;
for (const auto& x : a) {
if (b.count(x)) {
result.emplace(x);
}
}
return result;
}
};