-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaximum-length-of-repeated-subarray.cpp
47 lines (40 loc) · 1.3 KB
/
maximum-length-of-repeated-subarray.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
class Solution {
public:
//自己没有做出来,看了discuss做出来的
/**
* dp[i][j] = a[i] == b[j] ? 1 + dp[i + 1][j + 1] : 0;
* dp[i][j] : max lenth of common subarray start at a[i] & b[j];
*/
int findLength(vector<int>& A, vector<int>& B) {
int result=0;
vector<vector<int>> dp(A.size()+1,vector<int>(B.size()+1));
dp[A.size()][B.size()]=0;
for(int i=A.size()-1;i>=0;i--){
for(int j=B.size()-1;j>=0;j--){
dp[i][j]=(A[i]==B[j])?dp[i+1][j+1]+1:0;
result=max(result,dp[i][j]);
}
}
return result;
}
//暴力解法会超时
int findLength1(vector<int>& A, vector<int>& B) {
int result=0;
for(int i=0;i<A.size();i++){
vector<int> tmp;
for(int j=0;j<B.size();j++){
if(A[i]==B[j]) tmp.emplace_back(j);
}
for(int k=0;k<tmp.size();k++){
int len=0;
int aindex=i;
int bindex=tmp[k];
while(aindex<A.size() && bindex<B.size() && A[aindex]==B[bindex]){
aindex++,bindex++,len++;
}
result=max(result,len);
}
}
return result;
}
};