当前位置:网站首页>[dynamic planning] leetcode1092 Shortest common supersequence
[dynamic planning] leetcode1092 Shortest common supersequence
2022-06-10 06:58:00 【Twilight_ years】
1092. The shortest common supersequence
Given string a,b, seek a,b The shortest common supersequence of , namely a,b It is also a subsequence of this sequence and this sequence is the shortest .
The problem of converting bit editing distance
【 Dynamic programming 】 Section dp: Edit distance _ Twilight _ Blog years -CSDN Blog
The problem can be translated into :
Yes a Add operation , Make the added a contain b, Find the minimum number of additions .

explain :
If you want to a[i] After adding a character, do a contain b, Then we must make a[ 1 ~ i ] contain b[ 1~ j-1 ], namely under these circumstances dp[ i ] [ j ] = dp[ i ][ j - 1 ] + 1 .
If in a[i] You can do this without adding characters a contain b, So when a[ i ] ! =b[ j ] when , hold a[i] After removal a Can still contain b[j], So in this case ,dp[ i ][ j ] = dp[ i-1 ][ j ]
When a[ i ] = = b [ j ] when , Sure no need a[ i ] Take a match b[ j ],dp[ i ][ j ]=dp[ i -1 ][ j ], It can also be used. a [ i ] To match
b[ j ],dp[ i ][ j ]=dp[ i-1 ][ j- 1]
? use for Output the result in reverse order .
class Solution {
public:
string shortestCommonSupersequence(string a, string b) {
int n = a.size(), m = b.size(), INF = 1e8;
vector<vector<int>> f(n + 1, vector<int>(m + 1, INF));
for (int i = 0; i <= n; i ++ ) f[i][0] = 0;
for (int i = 1; i <= m; i ++ ) f[0][i] = i;
for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= m; j ++ ) {
f[i][j] = min(f[i][j - 1] + 1, f[i - 1][j]);
if (a[i - 1] == b[j - 1])
f[i][j] = min(f[i][j], f[i - 1][j - 1]);
}
string res;
for (int i = n, j = m; i >= 0;) {
if (!i || !j || a[i - 1] != b[j - 1]) {
if (j && f[i][j] == f[i][j - 1] + 1) {
res += b[ -- j];
} else {
if (i) res += a[ -- i];
else i -- ;
}
} else {
if (f[i][j] == f[i][j - 1] + 1) {
res += b[ -- j];
} else if (f[i][j] == f[i - 1][j]) {
res += a[ -- i];
} else {
res += a[ -- i];
j -- ;
}
}
}
reverse(res.begin(), res.end());
return res;
}
};
边栏推荐
- Arduino配置ESP32开发环境
- TAP 系列文章 2 | Tanzu Application Platform v1.1 安装配置步骤
- Qt--- create dialog box 3: implementation of shape variable dialog box
- Teleyecontroller v8.47 release changes socket framework
- 关于我并不是一个灰帽子的声明
- TeleyeControlor V8.16发布 完成注册表功能
- Efficiency improvement: realize personal task management and monitoring with notation
- P1073 [noip2009 improvement group] optimal trade problem solving hierarchical graph shortest path
- Ytu - C language exercises half search
- 一本通1258.数字金字塔 题解 动态规划
猜你喜欢

Yosezang original signature locator signaturetest v6.36 RLS release

Ros2+gazebo11+car+opencv line patrol recognition and speed steering control learning

Nextcloud internal server error the server cannot complete your request workaround

Multiple solutions to one problem × 5 (array + circular linked list)

Opengauss database ODBC environment connection configuration (Windows)

Qt--- create dialog box 3: implementation of shape variable dialog box

Tensorflow experiment IX ---------- Titanic

深度解析智能运维下告警关联频繁项集挖掘算法原理

智能合并视频,随机合并视频封面,并预设新标题

LabVIEW host computer and factory MES docking case communication program, supporting server and client
随机推荐
晨曦记账本记账,使用项目查看账目
微信团队分享:微信后台在海量并发请求下是如何做到不崩溃的
pyinstaller
POC_ Jenkins
Arduino configuring esp32 development environment
在 Kubernetes 中基于 StatefulSet 部署 MySQL(下)
26. difference between mouseenter and mouseover
Beyond compare
【计量经济学】工具变量估计与两阶段最小二乘法
LabVIEW控制Arduino实现红外测距(进阶篇—6)
Yosezang original signature locator signaturetest v6.36 RLS release
Scala fastjson modifying key or value
Jerry's aicmd_ SET_ BT_ Addr command format [chapter]
深度解析智能運維下告警關聯頻繁項集挖掘算法原理
Rk3399 default browser and Chinese language
Po ME21N enhanced control line text required
TeleyeControlor V8.7 修复广域网上线 增加显示延时功能
Local storage of JS data interaction
Tensorflow experiment IX ---------- Titanic
Wechat applet page returns and passes parameters