leetcode-最长公共子序列(二)-103

题目要求
在这里插入图片描述

思路
step 1:优先检查特殊情况。
step 2:获取最长公共子序列的长度可以使用动态规划,我们以dp[i][j]dp[i][j]dp[i][j]表示在s1中以iii结尾,s2中以jjj结尾的字符串的最长公共子序列长度。
step 3:遍历两个字符串的所有位置,开始状态转移:若是iii位与jjj位的字符相等,则该问题可以变成1+dp[i−1][j−1]1+dp[i-1][j-1]1+dp[i−1][j−1],即到此处为止最长公共子序列长度由前面的结果加1。
step 4:若是不相等,说明到此处为止的子串,最后一位不可能同时属于最长公共子序列,毕竟它们都不相同,因此我们考虑换成两个子问题,dp[i][j−1]dp[i][j-1]dp[i][j−1]或者dp[i−1][j]dp[i-1][j]dp[i−1][j],我们取较大的一个就可以了,由此感觉可以用递归解决。
step 5:但是递归的复杂度过高,重复计算了很多低层次的部分,因此可以用动态规划,从前往后加,由此形成一个表,表从位置1开始往后相加,正好符合动态规划的转移特征。
step 6:因为最后要返回该序列,而不是长度,所以在构造表的同时要以另一个二维矩阵记录上面状态转移时选择的方向,我们用1表示来自左上方,2表示来自左边,3表示来自上边。
step 7:获取这个序列的时候,根据从最后一位开始,根据记录的方向,不断递归往前组装字符,只有来自左上的时候才添加本级字符,因为这种情况是动态规划中两个字符相等的情况,字符相等才可用。
代码实现

class Solution {
  public:
    string x = "";
    string y = "";
    string LCS(string s1, string s2) {
        if (s1.length() == 0 || s2.length() == 0)
            return "-1";
        int len1 = s1.length();
        int len2 = s2.length();
        x = s1;
        y = s2;
        vector<vector<int>> dp(len1 + 1, vector<int>(len2 + 1, 0));
        vector<vector<int>> b(len1 + 1, vector<int>(len2 + 1, 0));
        for (int i = 1; i <= len1; i++) {
            for (int j = 1; j <= len2; j++) {
                if (s1[i - 1] == s2[j - 1]) {
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                    b[i][j] = 1;
                } else {
                    if (dp[i - 1][j] > dp[i][j - 1]) {
                        dp[i][j] = dp[i - 1][j];
                        b[i][j] = 2;
                    } else {
                        dp[i][j] = dp[i][j - 1];
                        b[i][j] = 3;
                    }
                }
            }
        }
        string res = ans(len1, len2, b);
        return res != "" ? res : "-1";
    }

    string ans(int i, int j, vector<vector<int>>& b) {
        string res = "";
        if (i == 0 || j == 0)
            return res;
        if (b[i][j] == 1) {
            res += ans(i - 1, j - 1, b);
            res += x[i - 1];
        } else if (b[i][j] == 2)
            res += ans(i - 1, j, b);
        else if (b[i][j] == 3)
            res += ans(i, j - 1, b);
        return res;
    }
};

相关推荐

  1. leetcode hot100公共序列

    2024-05-13 16:12:06       27 阅读
  2. Leetcode 1143 公共序列

    2024-05-13 16:12:06       49 阅读
  3. Leetcode 1143:公共序列

    2024-05-13 16:12:06       46 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-13 16:12:06       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-13 16:12:06       106 阅读
  3. 在Django里面运行非项目文件

    2024-05-13 16:12:06       87 阅读
  4. Python语言-面向对象

    2024-05-13 16:12:06       96 阅读

热门阅读

  1. 动态NAT

    动态NAT

    2024-05-13 16:12:06      33 阅读
  2. 猜数字(c++语言)

    2024-05-13 16:12:06       31 阅读
  3. 为什么数据库字符编码不一致会导致索引失效

    2024-05-13 16:12:06       32 阅读
  4. 【Git LFS】Git管理大文件

    2024-05-13 16:12:06       33 阅读
  5. lustre文件系统详细介绍

    2024-05-13 16:12:06       31 阅读
  6. Python基础学习之datetime模块

    2024-05-13 16:12:06       34 阅读
  7. Electron Forge | 跨平台实战详解(中)

    2024-05-13 16:12:06       31 阅读
  8. Ubuntu 系统中设置中文输入法

    2024-05-13 16:12:06       30 阅读