双字符串DP小结

作者: stepsma | 来源:发表于2016-12-02 23:48 被阅读0次

本文总结一下一些双字符串的DP问题。具体解法是要建立以两个字符串为边的矩阵。

vector<vector<int>> dp(lenA+1, vector<int>(lenB+1, 0)); \\ 要加1

dp[i][j] 为以A[i]结尾,和B[j]结尾的满足条件解。

    0 a b c d
0   
a
c
m
n

Longest Common Sub-sequence:

这道题的点是sub-sequence可以不连续。
dp[i][j] = 以A[i], B[j]为结尾,的最长sub-sequence长度。通项公式为

if(A[i] == B[j]) dp[i][j] = dp[i-1][j-1]+1;
else dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
int longestRepeatingSubsequence(string& str) {
        // Write your code here
        int len = str.length();
        vector<vector<int>> dp(len+1, vector<int>(len+1, 0));
        for(int i=1; i<=len; i++){
            for(int j=1; j<=len; j++){
                if(str[i-1] == str[j-1] && i != j){
                    dp[i][j] = 1 + dp[i-1][j-1];
                }else{
                    dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
                }
            }
        }
        return dp[len][len];
    }

Longest Common Substring:

这道题要点是sub-string必须连续,
dp[i][j] = 以A[i], B[j]为结尾,的最长sub-string长度 (必须包含A[i], B[j])。通项公式为:

if(A[i] == B[j]) dp[i][j] = dp[i-1][j-1] + 1;
else dp[i][j] = 0;

然后新添一个max_len变量,打擂台, 找最长。

int longestCommonSubstring(string &A, string &B) {
        // write your code here
        if(A.empty() || B.empty()) return 0;
        int lenA = A.size(), lenB = B.size();
        vector<vector<int>> dp(lenA+1, vector<int>(lenB+1, 0));
        int max_len = 0;
        for(int i=1; i<=lenA; i++){
            for(int j=1; j<=lenB; j++){
                if(A[i-1] == B[j-1]){
                    dp[i][j] = dp[i-1][j-1] + 1;
                }else{
                    dp[i][j] = 0;
                }
                if(dp[i][j] > max_len) max_len = dp[i][j];
            }
        }
        return max_len;
    }

Edit Distance

这道题的要点是下面的表达式。表示新加一个,删除一个,和edit一个。
dp[i][j]为以A[i], B[j]为结尾的最短edit distance。通项公式为:

dp[i][j] = min(dp[i-1][j-1], min(dp[i-1][j], dp[i][j-1])) + 1;
 int minDistance(string word1, string word2) {
        // write your code here
        if(word1.empty()) return word2.length();
        else if(word2.empty()) return word1.length();
        int lenA = word1.length(), lenB = word2.length();
        vector<vector<int>> dp(lenA+1, vector<int>(lenB+1, 0));
        for(int i=0; i<=lenA; i++){
            dp[i][0] = i;
        }
        for(int i=0; i<=lenB; i++){
            dp[0][i] = i;
        }
        for(int i=1; i<=lenA; i++){
            for(int j=1; j<=lenB; j++){
                if(word1[i-1] == word2[j-1]){
                    dp[i][j] = dp[i-1][j-1];
                }else{
                    dp[i][j] = min(dp[i-1][j-1], min(dp[i-1][j], dp[i][j-1])) + 1;
                }
            }
        }
        return dp[lenA][lenB];
    }

相关文章

  • 双字符串DP小结

    本文总结一下一些双字符串的DP问题。具体解法是要建立以两个字符串为边的矩阵。 dp[i][j] 为以A[i]结尾,...

  • DP小结

    DP种类 线性DP 区间DP 树形DP 背包DP01背包满背包完全背包(转成01背包) 例子:线性动规:拦截导弹,...

  • shell中各种括号()、(())、[]、[[]]、{}的作用

    技巧小结: 字符串比较用双中括号[[ ]];算数比较用单中括号[ ],左右留空格 算数运算用双小括号(( )) ;...

  • Shell | 各种括号的作用

    技巧小结:字符串比较用双中括号[[ ]]算数比较用单中括号[ ]——左右留空格算数运算用双小括号(( ))shel...

  • 97. 交错字符串

    97. 交错字符串 dp

  • 双法兰液位变送器DH3851DP怎么选型?

    双法兰液位变送器DH3851DP怎么选型? 双法兰液位变送器DH3851DP选型表: 双法兰液位变送器特点: a....

  • leetcode 712-dp

    C++ code://我们建立一个二维数组dp,其中dp[i][j]表示字符串s1的前i个字符和字符串s2的前j个...

  • Leetcode 5 求最长回文子序列

    题目介绍 给定一个字符串s,求最长的回文子序列。 Examples: Solution 解法一:DP 其中dp[j...

  • [LeetCode] 72. Edit Distance (h

    原题 思路:利用二维数组dp[i][j]存储状态: 从字符串A的0~i位子字符串 到 字符串B的0~j位子字符串,...

  • LeetCode 516. 最长回文子序列

    1、题目 2、分析 使用动态规划的方法。最重要的是明确dp数组的含义。定义dp[i][j] 为字符串第i个位置,到...

网友评论

    本文标题:双字符串DP小结

    本文链接:https://www.haomeiwen.com/subject/ncnqmttx.html