LeetCode 1092. Shortest Common Supersequence (2025/2/28每日一题)

今天的每日一题太简单了,就不记录了。之前2月28日的一直没写,今天补上:

标题:Shortest Common Supersequence

题目:

Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences. If there are multiple valid strings, return any of them.

A string s is a subsequence of string t if deleting some number of characters from t (possibly 0) results in the string s.

Example 1:

Input: str1 = "abac", str2 = "cab"
Output: "cabac"
Explanation: 
str1 = "abac" is a subsequence of "cabac" because we can delete the first "c".
str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac".
The answer provided is the shortest such string that satisfies these properties.

Example 2:

Input: str1 = "aaaaaaaa", str2 = "aaaaaaaa"
Output: "aaaaaaaa"

Constraints:

  • 1 <= str1.length, str2.length <= 1000
  • str1 and str2 consist of lowercase English letters.

解题思路:要求两个序列的最短公共超序列,则在构建超序列时需要保持原序列元素的相对位置。试想用两个指针分别在两个序列上遍历,按顺序将其元素添加进超序列中。但遍历的顺序怎么样呢?如何能满足“最短”的条件呢?很简单,要满足“最短”,则两个序列的公共元素只需要添加一次。这样就拆解成先求最长公共子序列的子问题了。

1、求最长公共子序列用动态规划算法:

  • 创建一个二维数组:dp[m+1][n+1],其中m和n分别是两个字符串的长度
  • 第一行第一列的dp[0][j], dp[i][0]都是0,因为空元素与其它序列的公共子序列肯定为空
  • 第二行和第二列开始,遍历整个dp矩阵,如果两个序列当前字符相等,则dp[i][j] 选取dp[i-1][j-1]+1, dp[i-1][j], dp[i][j-1]这三个值的最大值;如果两个序列当前字符不相等,则dp[i][j]选取dp[i-1][j]和dp[i][j-1]这两个值的最大值
  • 最后,dp[m][n]则为最长公共子序列的长度

2、通过步骤1中求出的dp矩阵结果,从右下角反向构建公共超序列。如果当前字符相等,直接添加到字符串中,如果不相等,选择dp[i][j+1]和dp[i+1][j] (这里i对应第一个字符串的(i-1)位)大的那个字符添加到结果字符串中。如下表所示:

dp矩阵cab
0000
a0011
b0012
a0012
c0112

str1 = "abac", str2="cab", 得出的dp矩阵如上表所示,在最右下角处,由于dp[i-1][j] > dp[i][j-1], 因此选择字符'c'放入超序列中,因为字符'b'是公共子序列中的字符,在j方向已经到了,需要遍历i方向一直到也遇到字符‘b’为止。

代码如下:

class Solution {
public:
    string shortestCommonSupersequence(string str1, string str2) {

        // 计算最长公共子序列的动态规划矩阵
        vector<vector<int>> dp(str1.length()+1, vector<int>(str2.length()+1, 0));
        for (int i = 1; i <= str1.length(); i++){
            for (int j = 1; j <= str2.length(); j++){
                if (str1[i-1] == str2[j-1]) {
                    dp[i][j] = max(dp[i-1][j-1]+1, max(dp[i-1][j], dp[i][j-1]));
                } else {
                    dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
                }
            }
        }
        
        // 构建最小公共超序列
        string res = "";
        int i = str1.length()-1, j = str2.length()-1;
        while(i >= 0 && j >= 0) {
            if (str1[i] == str2[j]) {
                res.push_back(str1[i--]);
                j--;
            } else if (dp[i][j+1] > dp[i+1][j]){
                res.push_back(str1[i--]);
            } else {res.push_back(str2[j--]);}
        }
        while(i >= 0) res.push_back(str1[i--]);
        while(j >= 0) res.push_back(str2[j--]);
        reverse(res.begin(), res.end());
        return res;
    }
};

时间和空间复杂度都是O(M*N)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值