每日一题 1143最长公共子序列(LCS)(灵神版本)

题目

题目

给定两个字符串 text1 和 text2,返回这两个字符串的最长 公共子序列 的长度。如果不存在 公共子序列 ,返回 0 。

一个字符串的 子序列 是指这样一个新的字符串:它是由原字符串在不改变字符的相对顺序的情况下删除某些字符(也可以不删除任何字符)后组成的新字符串。

例如,"ace" 是 "abcde" 的子序列,但 "aec" 不是 "abcde" 的子序列。

两个字符串的 公共子序列 是这两个字符串所共同拥有的子序列。

示例 1:

输入:text1 = "abcde", text2 = "ace"

输出:3

解释:最长公共子序列是 "ace" ,它的长度为 3 。

示例 2:

输入:text1 = "abc", text2 = "abc"

输出:3

解释:最长公共子序列是 "abc" ,它的长度为 3 。

示例 3:

输入:text1 = "abc", text2 = "def"

输出:0

解释:两个字符串没有公共子序列,返回 0 。

题解

回溯三问

  1. 当前操作:考虑s[i]和t[j]选或不选
  2. 子问题:前i个和前j个的LCS
  3. 下一个子问题:前i-1个和前j-1个;前i个和前j-1个;前i-1个和前j个

可以得到:
s[i] = t[j]时,dfs(i,j)=max(dfs(i,j-1),dfs(i-1,j),dfs(i-1,j-1)+1)
不满足s[i] = t[j]时,dfs(i,j)=max(dfs(i,j-1),dfs(i-1,j),dfs(i-1,j-1))

两个问题:
s[i] = t[j]时,需要dfs(i,j-1),dfs(i-1,j)吗? 答案是否定的

举个例子:

s=abcdc,t=abc,此时c都选,相当于s=abcd,t=ab的LCS(x)+1,此时dfs(i-1,j-1),

如果s=abcd,t=abc更优dfs(i,j-1),那么此时dfs(i,j-1)>x+1,

接下来s=abd,t=ab的LCS大于x,又因为此时的s,t为s=abcd,t=ab的子序列,所以s=abd,t=ab的LCS一定小于等于x,所以相互矛盾
不满足s[i] = t[j]时,需要dfs(i-1,j-1)吗 同样也是不需要的

dfs(i-1,j-1)的答案是包含在dfs(i,j-1)之中的,即dfs(i,j-1)>=dfs(i-1,j-1)

记忆化搜索

java 复制代码
class Solution {
    private char[] s,t;
    private int[][] cache;

    public int longestCommonSubsequence(String text1, String text2) {
        s = text1.toCharArray();
        t = text2.toCharArray();
        int n = s.length;
        int m = t.length;
        cache = new int[n][m];
        for (int i = 0; i < n; i++) {
            Arrays.fill(cache[i],-1);
        }
        return dfs(n - 1, m - 1);
    }

    public int dfs(int i, int j) {
        if (i < 0 || j < 0) {
            return 0;
        }
        if (cache[i][j] != -1) {
            return cache[i][j];
        }
        if (s[i] == t[j]) {
            return cache[i][j] = dfs(i - 1,j - 1) + 1;
        }
        return cache[i][j] = Math.max(dfs(i - 1, j),dfs(i, j - 1));
    }
}

递推

java 复制代码
class Solution {
    public int longestCommonSubsequence(String text1, String text2) {
        char[] s = text1.toCharArray();
        char[] t = text2.toCharArray();
        int n = s.length;
        int m = t.length;
        int[][] f = new int[n + 1][m + 1];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                f[i + 1][j + 1] = s[i] == t[j] ? f[i][j] + 1 :
                                    Math.max(f[i + 1][j], f[i][j + 1]);
            }
        }
        return f[n][m];
    }
}

空间优化

java 复制代码
class Solution {
    public int longestCommonSubsequence(String text1, String text2) {
        char[] t = text2.toCharArray();
        int m = t.length;
        int[] f = new int[m + 1];
        for (char x : text1.toCharArray()) {
            for (int j = 0, pre = 0; j < m; j++) {
                int temp = f[j + 1];
                f[j + 1] = x == t[j] ? pre + 1 : Math.max(f[j + 1], f[j]);
                pre = temp;
            }
        }
        return f[m];
    }
}
相关推荐
老鼠只爱大米1 天前
LeetCode算法题详解 1:两数之和
算法·leetcode·面试题·两数之和·two sum
欧阳天羲1 天前
ML工程师学习大纲
学习·算法·决策树
柒.梧.1 天前
深度解析Spring Bean生命周期以及LomBok插件
java·后端·spring
clorisqqq1 天前
人工智能现代方法 第一章绪论 笔记(2/2)
人工智能·笔记
AI爱好者20201 天前
智能优化算法2025年新书推荐——《智能优化算法及其MATLAB实例(第4版)》
开发语言·算法·matlab
初子无爱1 天前
Java接入支付宝沙箱支付教程
java·开发语言
菩提小狗1 天前
小迪安全2023-2024|第11天:基础入门-ChatGPT篇&注册体验&结合安全&融入技术&高效赋能&拓_笔记|web安全|渗透测试|网络安全
笔记·安全·chatgpt
程序猿零零漆1 天前
Spring之旅 - 记录学习 Spring 框架的过程和经验(十)基于注解配置的AOP使用
java·学习·spring
LYFlied1 天前
【每日算法】LeetCode215. 数组中的第K个最大元素
前端·算法
2501_941822751 天前
从限流降载到全链路流控的互联网工程语法实践与多语言探索
leetcode·模拟退火算法