1143. Longest Common Subsequence
发布日期:2021-05-04 20:03:49 浏览次数:21 分类:技术文章

本文共 1828 字,大约阅读时间需要 6 分钟。

题目:

Given two strings text1 and text2, return the length of their longest common subsequence.

subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.

 

If there is no common subsequence, return 0.

 

Example 1:

Input: text1 = "abcde", text2 = "ace" Output: 3  Explanation: The longest common subsequence is "ace" and its length is 3.

Example 2:

Input: text1 = "abc", text2 = "abc"Output: 3Explanation: The longest common subsequence is "abc" and its length is 3.

Example 3:

Input: text1 = "abc", text2 = "def"Output: 0Explanation: There is no such common subsequence, so the result is 0.

 

Constraints:

  • 1 <= text1.length <= 1000
  • 1 <= text2.length <= 1000
  • The input strings consist of lowercase English characters only.

 

思路:

经典DP题。设m为text1的长度,n为text2的长度,建立一个m*n的2-D矩阵。初始化是第一列和第一行,显然只要text1的第一个字母,和text2对上的第一个字母之后的所有[0][j]都是1,同理对上后的所有的[i][0]都为1,否则都应为初始值0。状态转移方程则很简单,对于所有i>=1和j>=1,首先dp[i][j]代表的是text1的前i位和text2前j位的最长公共子序列,那么有dp[i][j]=max(dp[i][j-1],dp[i-1][j]),即从两边各自少最后一个字母来看,选最大的作为备选项。如果text1[i]==text2[j],那么dp[i][j]=max(dp[i][j],dp[i-1][j-1]+1),即如果当前两个字母相等,则从两边同时退一位的格子中取出值,再加1,然后与原本上面比较完的值求最大即可。

 

代码:

class Solution {

public:
    int longestCommonSubsequence(string text1, string text2) {
        int m=text1.size(), n=text2.size();
        vector<vector<int>> dp(m,vector<int>(n));
        if(text1[0]==text2[0])
            dp[0][0]=1;
        for(int i=1;i<m;i++)
        {
            dp[i][0]=dp[i-1][0];
            if(text1[i]==text2[0])
                dp[i][0]=1;
        }
        for(int j=1;j<n;j++)
        {
            dp[0][j]=dp[0][j-1];
            if(text1[0]==text2[j])
                dp[0][j]=1;
        }
        for(int i=1;i<m;i++)
        {
            for(int j=1;j<n;j++)
            {
                dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
                if(text1[i]==text2[j])
                    dp[i][j]=max(dp[i][j], dp[i-1][j-1]+1);
            }
        }
        return dp[m-1][n-1];
    }
};

上一篇:81. Search in Rotated Sorted Array II
下一篇:516. Longest Palindromic Subsequence

发表评论

最新留言

表示我来过!
[***.240.166.169]2025年03月10日 16时03分23秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

对象和封装 2019-03-03
同时在写四门编程语言是怎样一种体验? 2019-03-03
【树形dp】P1273 有线电视网 2019-03-03
【分层图最短路】P4568 [JLOI2011]飞行路线 2019-03-03
【最短路】P4408 [NOI2003]逃学的小孩 2019-03-03
2020C证(安全员)模拟考试题及C证(安全员)模拟考试系统 2019-03-03
2020A证(安全员)模拟考试及A证(安全员)证考试 2019-03-03
2020电工(初级)考试及电工(初级)考试软件 2019-03-03
2020建筑电工(建筑特殊工种)实操考试视频及建筑电工(建筑特殊工种)作业模拟考试 2019-03-03
2020N1叉车司机模拟考试题库及N1叉车司机复审模拟考试 2019-03-03
2020熔化焊接与热切割考试及熔化焊接与热切割考试题库 2019-03-03
2020年G3锅炉水处理报名考试及G3锅炉水处理考试申请表 2019-03-03
2020年制冷与空调设备运行操作答案解析及制冷与空调设备运行操作考试总结 2019-03-03
2020年保育员(初级)考试资料及保育员(初级)新版试题 2019-03-03
2020年茶艺师(高级)考试内容及茶艺师(高级)考试申请表 2019-03-03
2021年烟花爆竹经营单位安全管理人员考试及烟花爆竹经营单位安全管理人员考试试卷 2019-03-03
2021年过氧化工艺试题及答案及过氧化工艺考试平台 2019-03-03
2021年重氮化工艺考试题库及重氮化工艺考试报名 2019-03-03
2021年车工(高级)考试总结及车工(高级)试题及答案 2019-03-03
2021年压力焊证考试及压力焊实操考试视频 2019-03-03