[LeetCode] 1092. Shortest Common Supersequence
2024-09-05 18:41:40
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;
}
};
最新文章
- 关于 IE6、 IE7兼容性总结(转)
- code review作业
- VIM自动补全插件 - YouCompleteMe--";大神级vim补全插件";
- [转载]解析用户生命周期价值:LTV
- Android webView解析URL参数
- LTE 切换过程中的数据切换
- PS太大GIMP可用
- 使用RMAN迁移文件系统数据库到ASM
- 7篇Model View和4篇双缓冲
- Dreamer2.1 发布 新增将Bean解析成xml和json
- Oracle / PLSQL函数 - NUMTODSINTERVAL和NUMTOYMINTERVAL
- C#操作Control异步工具类
- 如何激活已经运行过的Activity, 而不是重新启动新的Activity
- sql学习内容记录
- 磁性窗体设计C#(二)
- 基于Redisson实现分布式锁
- what&#39;s the python之自定义模块和包
- 安装 protoc 的各种坑
- PLSQL Developer概念学习系列之如何正确登录连接上Oracle(图文详解)
- java中checked和unchecked 异常处理
热门文章
- P1903 奖学金题解
- CentOS 7添加开机启动服务脚本
- java数据结构4--集合Set
- JAVA笔记18-容器之二增强的for循环(不重要)
- 使用net命令启动MongoDB服务发生系统错误,返回值为5
- C#与JAVA的互通
- 解决IDEA Initialization error &#39;https://start.spring.io&#39;
- TTTTTTTTTTTTTTTTT HDU 2586 How far away LCA的离线算法 Tarjan
- THU-CCF WC2019两开花记
- C# walls