-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path718.java
More file actions
74 lines (71 loc) · 2.45 KB
/
718.java
File metadata and controls
74 lines (71 loc) · 2.45 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
69
70
71
72
73
74
__________________________________________________________________________________________________
sample 10 ms submission
class Solution {
public int findLength(int[] A, int[] B) {
int ans = 0;
int[] dp = new int[B.length + 1];
for(int i = 0;i<A.length;i++){
for(int j = B.length - 1;j>=0;j--){
if(A[i] == B[j]){
dp[j+1] = dp[j] + 1;
if(ans < dp[j+1])
ans = dp[j+1];
}else
dp[j+1] = 0;
}
}
return ans;
}
}
__________________________________________________________________________________________________
sample 35692 kb submission
//Solution #4
import java.math.BigInteger;
class Solution {
int P = 113;
int MOD = 1_000_000_007;
int Pinv = BigInteger.valueOf(P).modInverse(BigInteger.valueOf(MOD)).intValue();
private int[] rolling(int[] source, int length) {
int[] ans = new int[source.length - length + 1];
long h = 0, power = 1;
if (length == 0) return ans;
for (int i = 0; i < source.length; ++i) {
h = (h + source[i] * power) % MOD;
if (i < length - 1) {
power = (power * P) % MOD;
} else {
ans[i - (length - 1)] = (int) h;
h = (h - source[i - (length - 1)]) * Pinv % MOD;
if (h < 0) h += MOD;
}
}
return ans;
}
private boolean check(int guess, int[] A, int[] B) {
Map<Integer, List<Integer>> hashes = new HashMap();
int k = 0;
for (int x: rolling(A, guess)) {
hashes.computeIfAbsent(x, z -> new ArrayList()).add(k++);
}
int j = 0;
for (int x: rolling(B, guess)) {
for (int i: hashes.getOrDefault(x, new ArrayList<Integer>()))
if (Arrays.equals(Arrays.copyOfRange(A, i, i+guess),
Arrays.copyOfRange(B, j, j+guess))) {
return true;
}
j++;
}
return false;
}
public int findLength(int[] A, int[] B) {
int lo = 0, hi = Math.min(A.length, B.length) + 1;
while (lo < hi) {
int mi = (lo + hi) / 2;
if (check(mi, A, B)) lo = mi + 1;
else hi = mi;
}
return lo - 1;
}
}
__________________________________________________________________________________________________