1. 程式人生 > >leetcode718+就是最長公共字串,Dp

leetcode718+就是最長公共字串,Dp

https://leetcode.com/problems/maximum-length-of-repeated-subarray/description/

class Solution {
public:
    int findLength(vector<int>& A, vector<int>& B) {
        int m = A.size(), n = B.size();
        int res = 0;
        int Dp[m+2][n+2];
        memset(Dp, 0, sizeof(Dp));
        for(int i=1; i<=m;i++){
            for(int j=1; j<=n; j++){
                if(A[i-1]==B[j-1]) Dp[i][j] = Dp[i-1][j-1]+1;
                else Dp[i][j] = 0;
                res = max(res, Dp[i][j]);
            }
        }
        return res;
    }
};