[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 <= 1000
str1
andstr2
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的更多相关文章
- [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 ...
随机推荐
- django middleware介绍
Middleware Middleware是一个镶嵌到django的request/response处理机制中的一个hooks框架.它是一个修改django全局输入输出的一个底层插件系统. 每个中间件 ...
- idea控制台搜索框
https://blog.csdn.net/honnyee/article/details/82772948
- NOIP2018提高组初赛准备
NOIP2017提高组初赛错题 一.单项选择题(共15 题,每题1.5 分,共计22.5 分:每题有且仅有一个正确选项) 4. 2017年10月1日是星期日,1949年10月1日是( ). A. 星期 ...
- 九、爬虫框架之Scrapy
爬虫框架之Scrapy 一.介绍 二.安装 三.命令行工具 四.项目结构以及爬虫应用简介 五.Spiders 六.Selectors 七.Items 八.Item Pipelin 九. Dowload ...
- Excel筛选操作
Excel的筛选操作如下: 选中指定列: 点击"开始"中的"排序和筛选" 点击如下小三角即可按条件进行筛选
- IOS上编译boost库
环境:xcode9mac os 10.13 1.下载boost库并解压2.cd到解压后的文件包内,执行 sh bootstrap.sh(解压后会得到这个脚本文件).3.执行./b2 编译整个库.4.经 ...
- UVALive 3211 : Now or later 【2-SAT】
题目链接 题意及题解参见lrj训练指南 #include<bits/stdc++.h> using namespace std; ; struct TwoSAT { int n; vect ...
- 新建com组件项目步骤
一.菜单栏 新建->项目->ATL->ATL项目->动态链接库 后续默认完成二.菜单栏 项目->添加类->ATL控件->“写入类的命名如:CeshiMai ...
- 18. ClustrixDB 管理CPU资源
作为一个分布式MPP数据库,ClustrixDB能够利用多个节点和核心来比单节点数据库更快地处理查询.有两个可调进程可以促进这一点. Clustrix选择了一组默认参数来控制那些提供最常见工作负载的进 ...
- latex beamer技巧
%章节标题\section{Related work(LSH)} %开始一页ppt \begin{frame}{Related work}{} \partitle{Locality-Sensitive ...