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. SQLServer Transaction Isolation Level

    基本用法 -- Syntax for SQL Server and Azure SQL Database SET TRANSACTION ISOLATION LEVEL { READ UNCOMMIT ...

  2. 你可能用到的Spring工具类?

    现在绝大部分项目都已经拥抱Spring生态,掌握Spring常用的工具类,是非常重要,零成本增加编码效率. 一.常用工具类 ObjectUtils org.springframework.util.O ...

  3. Tronado【第1篇】:tronado的简单使用以及使用

    Tronado Tornado 是 FriendFeed 使用的可扩展的非阻塞式 web 服务器及其相关工具的开源版本.这个 Web 框架看起来有些像web.py 或者 Google 的 webapp ...

  4. Eclipse中对一个项目进行复制粘贴为一个新项目

    1:对目标项目执行右键,选择“Copy”,然后在空白处右键,选择“Paste”结果如下图: 2:右键新项目,点击Properties, 3:打开Navigator视图 4:打开.settings文件夹 ...

  5. 关于react中context的使用

    context是用于组件间数据的传递,就是减少props的使用 具体使用也很简单 第一步设置默认值,第二部用provider发放,第三步使用contextType来接受最近的provider,然后直接 ...

  6. 【NOIP2016提高A组模拟9.15】Osu

    题目 分析 考虑二分答案, 二分小数显然是不可取的,那么我们将所有可能的答案求出来,记录在一个数组上,排个序(C++调用函数很容易超时,手打快排,时间复杂度约为\(O(>8*10^7)\),但相 ...

  7. 网络吞吐量(network)

    题目 分析 过一遍spfa,把从点1到其他每一个点的最短路求出来, 接着递归把所有最短路径上的路径保留,其他的删掉. 对于保留的路径作为网络流的边,流量为无穷大,对于每个点拆点两个点之间的流量为吞吐量 ...

  8. JavaScript 中的 for 循环---------------引用

    在ECMAScript5(简称 ES5)中,有三种 for 循环,分别是: 简单for循环 for-in forEach 下面先来看看大家最常见的一种写法: 当数组长度在循环过程中不会改变时,我们应将 ...

  9. php实现hashTable

    Hash表作为最重要的数据结构之一,也叫做散列表.使用PHP实现Hash表的功能.PHP可以模拟实现Hash表的增删改查.通过对key的映射到数组中的一个位置来访问.映射函数叫做Hash函数,存放记录 ...

  10. luogu 4059 [Code+#1]找爸爸 动态规划

    Description 小A最近一直在找自己的爸爸,用什么办法呢,就是DNA比对.小A有一套自己的DNA序列比较方法,其最终目标是最 大化两个DNA序列的相似程度,具体步骤如下:1.给出两个DNA序列 ...