[LeetCode] 1092. Shortest Common Supersequence
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 <= str1.length, str2.length <= 1000str1andstr2consist 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的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- [LeetCode] 243. Shortest Word Distance 最短单词距离
Given a list of words and two words word1 and word2, return the shortest distance between these two ...
- [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 ...
- [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 ...
- 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 ...
- 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. ...
- 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 ...
随机推荐
- Mockplus、Axure、墨刀软件对比
Mockplus 优点:基础版免费使用,操作简单,上手快,交互简单(只需拖曳就可以),功能多样,组件资源丰富,预览方式和导出类型多样,支持团队协作. 缺点:不支持手势交互. Axure 优点:操作变化 ...
- DevExpress WPF v19.1新版亮点:Gantt/Map控件新功能
行业领先的.NET界面控件DevExpress 日前正式发布v19.1版本,本站将以连载的形式介绍各版本新增内容.在本系列文章中将为大家介绍DevExpress WPFv19.1中新增的一些控件及部分 ...
- 安装虚拟环境 virtualenv
前言 玩python 当然需要安装虚拟环境,这样就万一有多个项目,避免会冲突,如何安装 virtualenv: 下面如下: 输入 pip list cmd 你的电脑: 然后 输入 pip list: ...
- ks代码助解
代码实现: data_test_2 = {'gd':[1,1,1,1,1,1,0,0,0,0,0,0,0],'score':[1,2,0,2,2,7,4,5,4,0,4,18,np.nan]} dat ...
- notepad++文档格式转化window、unix、MAC
切换
- python之路day14--嵌套函数、匿名函数、高阶函数。函数的递归
嵌套函数 函数里不仅可以写代码,还可以嵌套函数 name = "小猿圈" def change(): name = "小猿圈,自学编程" def chang ...
- 解决jmeter 请求参数中文乱码
今天在用jmeter 写脚本时发现查看结果树request post请求中文参数值是乱码,故记录下解决过程. 解决过程如下: 1.修改本地配置文件 因为此处的数据,还没有发送出去,所以,肯定是这个变量 ...
- 查看是否安装jdk及路径
JDK能否曾经装置,可以在cmd窗口里输出java -version,假定没有提示出错,就表示曾经装置. JDK的装置途径,可以输出java -verbose,会前往很多信息,其中就包括了JDK的装置 ...
- Hadoop2.7.4 yarn(HA)集群搭建步骤(CentOS7)
群节点分配: Park01:Zookeeper.NameNode(active).ResourceManager(active) Park02:Zookeeper.NameNode(standby) ...
- 对JavaScript 引擎基础:原型优化的研究 -----------------------引用
一.优化层级与执行效率的取舍 介绍了现代 JavaScript 引擎通用的工作流程: 我们也指出,尽管从高级抽象层面来看,引擎之间的处理流程都很相似,但他们在优化流程上通常都存在差异.为什么呢?为什么 ...