-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLC_718_MaxLenOfRepeatedSubArray.cpp
221 lines (201 loc) · 6.31 KB
/
LC_718_MaxLenOfRepeatedSubArray.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
718. Maximum Length of Repeated Subarray
https://leetcode.com/problems/maximum-length-of-repeated-subarray/
*/
class Solution {
public:
int n1,n2;
typedef unsigned long long ll;
const int MOD = 1e9+7;
const int BASE = 107;
vector<ll> powers, hash1, hash2;
int findLength(vector<int>& nums1, vector<int>& nums2) {
n1 = nums1.size(); n2 = nums2.size();
//return solveByLCS(nums1, nums2); //LC substring
//return solveByString(nums1, nums2);
//return solveByHash1(nums1, nums2);
return solveByHash2(nums1, nums2);
}//end
int solveByHash2(vector<int>& nums1, vector<int>& nums2)
{
int l=0, r=min(n1, n2);
powers = {1};
for(int i=0; i<max(n1, n2); i++)
powers.push_back(powers.back() * BASE%MOD); // computes power of base.
// hash1.assign(n1+1, 0);
// for(int i=0; i<n1; i++)
// hash1[i+1] = hash1[i]*BASE + nums1[i];
// hash2.assign(n2+1, 0);
// for(int i=0; i<n2; i++)
// hash2[i+1] = hash2[i]*BASE + nums2[i];
// for(int i=0; i<powers.size(); i++) cout<<powers[i]<<" "; cout<<endl;
while(l<=r)
{
int mid = (l+r)/2;
// cout<<"->"<<l<<"L "<<r<<"R "<<mid<<"M "<<endl;
if(checkByHash1(nums1, nums2, mid))
l = mid+1; // try to expand size
else
r = mid-1; // try to shrink size
}
return l-1;
}
ll getHash(int left, int right, const vector<ll>& hash)
{
return hash[right+1] - hash[left]*powers[right-left+1];
}
bool checkByHash2(vector<int>& nums1, vector<int>& nums2, int& len)
{
unordered_map<ll, vector<int>> seen;
for(int i=0; i<=n1-len; i++)
{
ll h = getHash(i, i+len-1, hash1);
seen[h].push_back(i);
}
for(int i=0; i<=n2-len; i++)
{
ll h = getHash(i, i+len-1, hash2);
if (!seen.count(h)) continue;
for (int j : seen[h]) // Double check - This rarely happens when collision occurs -> No change in time complexity
if (equal(nums1.begin()+j, nums1.begin()+j+len, nums2.begin()+i, nums2.begin()+i+len))
return true;
}
return false;
}
bool checkByHash1(vector<int>& nums1, vector<int>& nums2, int& len)
{
unordered_set<ll> firstHash = roolingHash(nums1, len);
unordered_set<ll> secHash = roolingHash(nums2, len);
// for(auto s: firstHash) cout<<s<<" "; cout<<endl;
// for(auto s: secHash) cout<<s<<" "; cout<<endl;
for(const ll& curHash: secHash)
{
if(firstHash.count(curHash)) return true;
}
return false;
}
unordered_set<ll> roolingHash(vector<int>& nums, int& len)
{
int n = nums.size();
ll hash=0;
unordered_set<ll> ans;
for(int i=0; i<len; i++)
hash = (hash + (nums[i]*powers[len-i])%MOD)%MOD;
ans.insert(hash);
for(int i=len; i<n; i++)
{
hash = (hash + MOD - (nums[i-len]*powers[len])%MOD) %MOD; //remove prev char
hash = (hash * BASE)%MOD; // shift one base left
hash = (hash + (nums[i]*BASE)%MOD)%MOD; // add cur char;
ans.insert(hash);
}
return ans;
}
/*
int solveByHash1(vector<int>& nums1, vector<int>& nums2)
{
int l=0, mid;
int r = min(n1, n2);
while(l+1<r)
{
mid = l + (r-l)/2;
if(isGood(nums1, nums2, mid))
{
l = mid;
}
else r = mid;
}
if(isGood(nums1, nums2, r)) return r;
if(isGood(nums1, nums2, l)) return l;
return 0;
}
bool isGood(vector<int>& nums1, vector<int>& nums2, int len) {
// if(n1<len || n2<len) return false;
auto a = findHash(nums1, len);
auto b = findHash(nums2, len);
for(auto &c : a)
{
if(b.count(c.first)) return true;
}
return false;
}
ll base = 683303;
ll mod = 1e9+1;
unordered_map<ll, ll> findHash(vector<int> nums, int len)
{
ll p = 1;
ll hash = 0;
for(int i=len-1; i>=0; i--){
hash += ((p%mod)*(nums[i]%mod))%mod;
hash %=mod;
if(i!=0)
p = ((p%mod)*(base%mod))%mod;
}
unordered_map<ll,ll> mp;
mp[hash]++;
int l=0, r=len, n = nums.size();
while(r<n){
hash = (hash+mod - ((p%mod)*(nums[l]%mod))%mod)%mod;
hash = ((hash%mod) * (base%mod))%mod;
hash += nums[r];
hash %= mod;
l++; r++;
mp[hash]++;
}
return mp;
}
*/
/*
string stringify(vector<int> &nums)
{
string out;
for(int &x: nums) out+=x+'0';
return out;
}
int solveByString(vector<int>& nums1, vector<int>& nums2) {
n1 = nums1.size(); n2 = nums2.size();
string strA = stringify(nums1), strB = stringify(nums2);
int l=0, r=min(n1, n2)+1;
while(l<r)
{
int mid = (l+r)/2;
// cout<<"->"<<l<<"L "<<r<<"R "<<mid<<"M "<<endl;
if(checkBySet(strA, strB, mid))
l = mid+1;
else
r = mid;
}
return r-1;
}//end
bool checkBySet(string& a, string &b, int len)
{
unordered_set<string> st;
for(int i=0; i<=n1-len; i++)
st.insert(a.substr(i, len));
// for(string s: st) cout<<s<<" "; cout<<endl;
for(int i=0; i<=n2-len; i++){
if(st.count(b.substr(i, len))) return true;
}
return false;
}
*/
/*
int solveByLCS(vector<int>& nums1, vector<int>& nums2) {
n1 = nums1.size(); n2 = nums2.size();
vector<vector<int>> dp(n1+1, vector<int>(n2+1, 0));
int ans=0;
for(int i = 1; i<=n1; i++)
{
for(int j = 1; j<=n2; j++)
{
if(nums1[i-1] == nums2[j-1])
{
dp[i][j] = 1 + dp[i-1][j-1];
ans = max(ans, dp[i][j]);
}
}
}
return ans;
}//end
*/
};