Leetcode 718. Maximum Length of Repeated Subarray | 公共连续子串的最大长度

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

class Solution {
public:
    int findLength(vector<int>& A, vector<int>& B) {
        int dp[A.size()+1][B.size()+1];
        int ret = 0;
        for (int i = 0; i < A.size(); i++) {
            for (int j = 0; j < B.size(); j++) {
                if (i == 0 || j == 0) {
                    dp[i][j] = A[i] == B[j];
                } else {
                    // dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
                    dp[i][j] = A[i] == B[j];
                    if (A[i] == B[j]) {
                        dp[i][j] = max(dp[i][j], dp[i-1][j-1] + 1);
                    }
                }
                // cout << "i:" << i << " j:" << j << " dp:" << dp[i][j] << endl;
                ret = max(ret, dp[i][j]);
            }
        }
        return ret;
    }
};


猜你喜欢

转载自blog.csdn.net/u011026968/article/details/80920918
今日推荐