int findLength(vector<int>& A, vector<int>& B) {
if(A.empty() || B.empty()) return 0;
int NA = A.size(), NB = B.size();
int res = 0;
vector<int> dp(NB,0);
for(int j=0; j<NB; j++) {
if(A[0] == B[j]) {
res = 1;
dp[j] = 1;
}
}
vector<int> dp1 = dp;
for(int i=1; i<NA; i++) {
for(int j=0; j<NB; j++) {
if(A[i] == B[j]) {
if(j == 0) {
dp[j] = 1;
}
else {
dp[j] = dp1[j-1]+1;
res = max(dp[j], res);
}
}
else dp[j] = 0;
}
dp1 = dp;
}
return res;
}
No comments:
Post a Comment