-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP_4.cpp
More file actions
68 lines (61 loc) · 1.62 KB
/
Copy pathP_4.cpp
File metadata and controls
68 lines (61 loc) · 1.62 KB
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
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll MOD = 1L << 39;
int BASE = 100003;
ll getHash(ll* hash, ll* pow, int l, int r) {
return (hash[r + 1] - ((__int128)hash[l] * pow[r - l + 1]) % MOD + MOD) % MOD;
}
int longestCommonSubpath(int f, vector<vector<int>>& paths) {
int n = (int)paths.size();
int minL = (int)1e9;
for (auto p : paths) {
minL = min(minL, (int)p.size());
}
ll pow[minL + 1];
pow[0] = 1L;
for (int i = 1; i <= minL; i++) {
pow[i] = pow[i - 1] * BASE % MOD;
}
ll** hash = new ll*[n];
for (int i = 0; i < n; i++) {
int m = paths[i].size();
hash[i] = (ll*)calloc(m + 1, sizeof(ll));
for (int j = 1; j <= m; j++) {
hash[i][j] = (hash[i][j - 1] * BASE + (paths[i][j - 1] + 1)) % MOD;
}
}
int lo = 0;
int hi = minL;
while (lo < hi) {
int mid = (lo + ((hi - lo) >> 1) + 1);
int ok = 1;
unordered_set<ll>* row = nullptr;
for (int i = 0; i < n; i++) {
if (row == nullptr) {
row = new unordered_set<ll>();
for (int j = 0; j <= (int)paths[i].size() - mid; j++) {
row->emplace(getHash(hash[i], pow, j, j + mid - 1));
}
} else {
unordered_set<ll>* next = new unordered_set<ll>;
for (int j = 0; j <= (int)paths[i].size() - mid; j++) {
if (row->count(getHash(hash[i], pow, j, j + mid - 1))) {
next->emplace(getHash(hash[i], pow, j, j + mid - 1));
}
}
row = next;
if (row->empty()) {
ok = 0;
break;
}
}
}
if (ok) {
lo = mid;
} else {
hi = mid - 1;
}
}
return lo;
}