Skip to content

Commit 3915e32

Browse files
authoredMay 20, 2022
feat: add solutions to lc problem: No.1923
No.1923.Longest Common Subpath
1 parent 7eb6955 commit 3915e32

File tree

3 files changed

+111
-2
lines changed

3 files changed

+111
-2
lines changed
 

‎solution/1900-1999/1923.Longest Common Subpath/README.md

+37-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,43 @@
6666
<!-- 这里可写当前语言的特殊实现逻辑 -->
6767

6868
```python
69-
69+
class Solution:
70+
def longestCommonSubpath(self, n: int, paths: List[List[int]]) -> int:
71+
def get(l, r, h):
72+
return (h[r] - h[l - 1] * p[r - l + 1]) % mod
73+
74+
def check(l):
75+
cnt = Counter()
76+
for k, path in enumerate(paths):
77+
vis = set()
78+
for i in range(len(path) - l + 1):
79+
j = i + l - 1
80+
x = get(i + 1, j + 1, hh[k])
81+
if x not in vis:
82+
vis.add(x)
83+
cnt[x] += 1
84+
return max(cnt.values()) == len(paths)
85+
86+
base = 133331
87+
mod = 2**64+1
88+
p = [0] * 100010
89+
p[0] = 1
90+
for i in range(1, len(p)):
91+
p[i] = (p[i - 1] * base) % mod
92+
hh = []
93+
for path in paths:
94+
h = [0] * (len(path) + 10)
95+
for j, c in enumerate(path):
96+
h[j + 1] = (h[j] * base) % mod + c
97+
hh.append(h)
98+
left, right = 0, min(len(path) for path in paths)
99+
while left < right:
100+
mid = (left + right + 1) >> 1
101+
if check(mid):
102+
left = mid
103+
else:
104+
right = mid - 1
105+
return left
70106
```
71107

72108
### **Java**

‎solution/1900-1999/1923.Longest Common Subpath/README_EN.md

+37-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,43 @@
5858
### **Python3**
5959

6060
```python
61-
61+
class Solution:
62+
def longestCommonSubpath(self, n: int, paths: List[List[int]]) -> int:
63+
def get(l, r, h):
64+
return (h[r] - h[l - 1] * p[r - l + 1]) % mod
65+
66+
def check(l):
67+
cnt = Counter()
68+
for k, path in enumerate(paths):
69+
vis = set()
70+
for i in range(len(path) - l + 1):
71+
j = i + l - 1
72+
x = get(i + 1, j + 1, hh[k])
73+
if x not in vis:
74+
vis.add(x)
75+
cnt[x] += 1
76+
return max(cnt.values()) == len(paths)
77+
78+
base = 133331
79+
mod = 2**64+1
80+
p = [0] * 100010
81+
p[0] = 1
82+
for i in range(1, len(p)):
83+
p[i] = (p[i - 1] * base) % mod
84+
hh = []
85+
for path in paths:
86+
h = [0] * (len(path) + 10)
87+
for j, c in enumerate(path):
88+
h[j + 1] = (h[j] * base) % mod + c
89+
hh.append(h)
90+
left, right = 0, min(len(path) for path in paths)
91+
while left < right:
92+
mid = (left + right + 1) >> 1
93+
if check(mid):
94+
left = mid
95+
else:
96+
right = mid - 1
97+
return left
6298
```
6399

64100
### **Java**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution:
2+
def longestCommonSubpath(self, n: int, paths: List[List[int]]) -> int:
3+
def get(l, r, h):
4+
return (h[r] - h[l - 1] * p[r - l + 1]) % mod
5+
6+
def check(l):
7+
cnt = Counter()
8+
for k, path in enumerate(paths):
9+
vis = set()
10+
for i in range(len(path) - l + 1):
11+
j = i + l - 1
12+
x = get(i + 1, j + 1, hh[k])
13+
if x not in vis:
14+
vis.add(x)
15+
cnt[x] += 1
16+
return max(cnt.values()) == len(paths)
17+
18+
base = 133331
19+
mod = 2**64+1
20+
p = [0] * 100010
21+
p[0] = 1
22+
for i in range(1, len(p)):
23+
p[i] = (p[i - 1] * base) % mod
24+
hh = []
25+
for path in paths:
26+
h = [0] * (len(path) + 10)
27+
for j, c in enumerate(path):
28+
h[j + 1] = (h[j] * base) % mod + c
29+
hh.append(h)
30+
left, right = 0, min(len(path) for path in paths)
31+
while left < right:
32+
mid = (left + right + 1) >> 1
33+
if check(mid):
34+
left = mid
35+
else:
36+
right = mid - 1
37+
return left

0 commit comments

Comments
 (0)