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;
}
};

  

  

[LeetCode] 1092. Shortest Common Supersequence的更多相关文章

  1. [leetcode]244. Shortest Word Distance II最短单词距离(允许连环call)

    Design a class which receives a list of words in the constructor, and implements a method that takes ...

  2. [LeetCode] 235. Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最近公共祖先

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  3. [LeetCode] 236. Lowest Common Ancestor of a Binary Tree 二叉树的最近公共祖先

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

  4. [LeetCode] 243. Shortest Word Distance 最短单词距离

    Given a list of words and two words word1 and word2, return the shortest distance between these two ...

  5. [LeetCode] 244. Shortest Word Distance II 最短单词距离 II

    This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...

  6. [LeetCode] 245. Shortest Word Distance III 最短单词距离 III

    This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...

  7. hdu-1941 Find the Shortest Common Superstring

    The shortest common superstring of 2 strings S 1 and S 2 is a string S with the minimum number of ch ...

  8. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  9. leetcode@ [236] Lowest Common Ancestor of a Binary Tree(Tree)

    https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ Given a binary tree, find the ...

随机推荐

  1. java高并发实战Netty+协程(Fiber)|系列1|事件驱动模式和零拷贝

    今天开始写一些高并发实战系列. 本系列主要讲两大主流框架: Netty和Quasar(java纤程库) 先介绍netty吧,netty是业界比较成熟的高性能异步NIO框架. 简单来说,它就是对NIO2 ...

  2. linux 生成密钥和公钥,实现免密登录

    1. 在相应的用户根目录下生成密钥公钥,输入如下命令: ssh-keygen -t rsa 2. 直接三次回车:会生成两个文件:id_rsa / id_rsa.pub,分别为密钥和公钥 3.  打开公 ...

  3. 重大更新:DeepFaceLab更新至2019.12.20

    本次更新增加SAEHD:lr_dropout参数,训练时可以打开或者禁用(默认禁用),每次换脸经过足够的训练后可以启用此选项以减少重复次数,从而获得额外的清晰度.还有一个比较有意义的更新是增加了图片元 ...

  4. Jmeter启动jmeter-server.bat 报java.io.FileNotFoundException:rmi_keystore.jks 解决方法

    解决方法:1.找到apache-jmeter-5.0\bin\jmeter.properties 2.修改server.rmi.ssl.disable=true (记得去除server.rmi.ssl ...

  5. scikit-plot

    安装说明 安装Scikit-plot非常简单,直接用命令: pip install scikit-plot 即可完成安装. 仓库地址: https://github.com/reiinakano/sc ...

  6. UIScrollView的简单使用

    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds]; //将scrollView添加到当前 ...

  7. mysql 查询碎片的方法

    mysql 查询碎片的方法 mysql length,engine,data_free,table_rows group by table_name order by table_rows asc; ...

  8. java——>> 和>>>

    试一下 public static void main(String[] args) { System.out.println(Integer.toBinaryString(-16)); System ...

  9. 对elementui整体设计分析-------引用

    1.需求分析 丰富的 feature:丰富的组件,自定义主题,国际化. 文档 & demo:提供友好的文档和 demo,维护成本小,支持多语言. 安装 & 引入:支持 npm 方式和 ...

  10. sqlserver 返回刚插入的那条数据

    insert into xxxxxx(Col_002,UrgentStatus,DoWorkShop,Col_004,Col_005,Col_006,Col_003,Col_007,postQQ,Co ...