[LeetCode] 1092. Shortest Common Supersequence

LeetCode刷题记录

传送门

Description

Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them.

(A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywherefrom T) results in the string S.)

 

Example 1:

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

 

Note:

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

思路

题意:构造一个字符串,使得其子序列同时包含有 str1 和 str2,要求这个字符串在满足要求情况下长度最短

题解:找出 str1 和 str2 的最长公共子序列,剩余不在最长公共子序列中的字符拼接到这个最长公共子序列中。

 

class Solution {
public:
    string shortestCommonSupersequence(string str1, string str2) {
        string res = "";
        int len1 = str1.size(), len2 = str2.size();
        int dp[len1 + 5][len2 + 5];
        memset(dp, 0, sizeof(dp));
        int i, j;
        for (i = 1; i <= len1; i++){
            for (j = 1; j <= len2; j++){
                if (str1[i - 1] == str2[j - 1])    dp[i][j] = dp[i - 1][j - 1] + 1;
                else    dp[i][j] = dp[i][j - 1] > dp[i - 1][j] ? dp[i][j - 1] : dp[i - 1][j];
            }
        }
        i = len1, j = len2;
        string common = "";
        while (dp[i][j]){
            if (dp[i][j] == dp[i - 1][j])   i--;
            else if (dp[i][j] == dp[i][j - 1])  j--;
            else    common += str1[i - 1], i--, j--;
        }

        reverse(common.begin(), common.end());

        int len3 = common.size();
        i = 0, j = 0;
        for (int k = 0; k < len3; k++){
            while (i < len1 && str1[i] != common[k]){
                res += str1[i++];
            }
            while (j < len2 && str2[j] != common[k]){
                res += str2[j++];
            }
            res += common[k];
            i++;
            j++;
        }
        while (i < len1){
            res += str1[i++];
        }
        while (j < len2){
            res += str2[j++];
        }
        return res;
    }
};

  

  

转载于:https://www.cnblogs.com/ZhaoxiCheung/p/leetcode-shortest-common-supersequence.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值