当前位置:网站首页>Longest common subsequence (LCS) (dynamic programming, recursive)
Longest common subsequence (LCS) (dynamic programming, recursive)
2022-07-07 05:08:00 【Madness makes freedom】
1) Recursion after going :
- State Design :dp[i][j] Indicates that two sequences start from the first position , The first sequence goes to i-1 Location , The second to j-1 The longest common subsequence of positions ;
- State transition equation :dp[i][j]=dp[i-1][j-1]+1 (str1[i-1]==str2[j-1]);
- || = max(dp[i-1][j],dp[i][j-1]) (str1[i-1]!=str2[j-1]);
- The border :dp[0][j]=dp[i]=0;(i=0...len1,j=0...len2);
- Finally, according to dp[i][j] Output the content of the longest common subsequence , When dp[i][j]== (dp[i-1][j] || dp[i][j-1] when , obvious str1[i]!=str[j]; When dp[i][j]==dp[i-1][j-1] when , obvious str1[i]==str2[j], Then add this element to str in .
- Of course , If you recurs after going , The subscript of the character is from 1 At the beginning , Then state design can be expressed in this way : State Design :dp[i][j] Indicates that two sequences start from the first position , The first sequence goes to i Location , The second to j The longest common subsequence of positions ; And below me The character subscript entered by the program is from 0 Start typing .
/**
1) State Design :dp[i][j] Indicates that two sequences start from the first position , The first sequence goes to i-1 Location ,
The second to j-1 The longest common subsequence of positions ;
State transition equation :dp[i][j]=dp[i-1][j-1]+1 (str1[i-1]==str2[j-1]);
|| = max(dp[i-1][j],dp[i][j-1]) (str1[i-1]!=str2[j-1]);
The border :dp[0][j]=dp[i]=0;(i=0...len1,j=0...len2);
Finally, according to dp[i][j] Output the content of the longest common subsequence , When dp[i][j]==
(dp[i-1][j] || dp[i][j-1] when , obvious str1[i]!=str[j]; When dp[i][j]
==dp[i-1][j-1] when , obvious str1[i]==str2[j], Then add this element to
str in .
Of course , If you recurs after going , The subscript of the character is from 1 At the beginning , So state design
It can be expressed in this way : State Design :dp[i][j] Indicates that two sequences start from the first position ,
The first sequence goes to i Location , The second to j The longest common subsequence of positions ; And below me
The character subscript entered by the program is from 0 Start typing .
*/
/**
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
string str1,str2;
cout << "input two strings:\n";
cin >> str1 >> str2;
int len1=str1.size(),len2=str2.size();
int dp[len1+1][len2+1]; //dp[i][j] Indicates that two sequences start from the first position , The first sequence goes to i-1 Location , The second to j-1 Location
string str; // The longest common subsequence of
for(int i=0;i<=len1;++i)
dp[i][0]=0;
for(int j=0;j<=len2;++j)
dp[0][j]=0;
for(int i=1;i<=len1;++i)
for(int 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]=max(dp[i-1][j],dp[i][j-1]);
}
for(int i=len1,j=len2;i>0&&j>0;) // Seems to have two point Shadow
{
if(dp[i][j]==dp[i-1][j])
--i;
else if(dp[i][j]==dp[i][j-1])
--j;
else
{
str=str1[i-1]+str;
--i;
--j;
}
}
cout << dp[len1][len2] << endl;
cout << str << endl;
cout << "Hello world!" << endl;
return 0;
}
*/
2) Push back and forth ;
State Design :dp[i][j] Indicates that two sequences start from the tail position , The first sequence goes to i Location ,
The second to j The longest common subsequence of positions ;
State transition equation :dp[i][j]=dp[i+1][j+1]+1 (str1[i]==str2[j]);
|| = max(dp[i+1][j],dp[i][j+1]) (str1[i]!=str2[j]);
Finally, according to dp[i][j] Output the content of the longest common subsequence , When dp[i][j]==
(dp[i+1][j] || dp[i][j+1] when , obvious str1[i]!=str[j]; When dp[i][j]
==dp[i-1][j-1] when , obvious str1[i]==str2[j].
/**
2) Push back and forth ;
State Design :dp[i][j] Indicates that two sequences start from the tail position , The first sequence goes to i Location ,
The second to j The longest common subsequence of positions ;
State transition equation :dp[i][j]=dp[i+1][j+1]+1 (str1[i]==str2[j]);
|| = max(dp[i+1][j],dp[i][j+1]) (str1[i]!=str2[j]);
Finally, according to dp[i][j] Output the content of the longest common subsequence , When dp[i][j]==
(dp[i+1][j] || dp[i][j+1] when , obvious str1[i]!=str[j]; When dp[i][j]
==dp[i-1][j-1] when , obvious str1[i]==str2[j].
*/
/**
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
string str1,str2;
cout << "input two strings:\n";
cin >> str1 >> str2;
int len1=str1.size(),len2=str2.size();
int dp[len1+1][len2+1]={0}; //dp[i][j] Indicates that two sequences start from the tail position , The first sequence goes to i Location , The second to j Location
string str; // The longest common subsequence of
for(int i=0;i<=len1;++i)
dp[i][0]=0;
for(int j=0;j<=len2;++j)
dp[0][j]=0;
for(int i=len1-1;i>=0;--i)
for(int j=len2-1;j>=0;--j)
{
if(str1[i]==str2[j])
dp[i][j]=dp[i+1][j+1]+1;
else
dp[i][j]=max(dp[i+1][j],dp[i][j+1]);
}
for(int i=0,j=0;i<len1&&j<len2;) // Seems to have two point Shadow
{
if(dp[i][j]==dp[i+1][j])
++i;
else if(dp[i][j]==dp[i][j+1])
++j;
else
{
str+=str1[i];
++i;
++j;
}
}
cout << dp[0][0] << endl;
cout << str << endl;
cout << "Hello world!" << endl;
return 0;
}
*/
Recursive solution :
/**
3) Recursive solution
*/
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
string str1,str2,str;
int dp[100][100];
int lcs_len(int l1,int l2);
void lcs_str(int l1,int l2);
int main()
{
cout << "input two strings:\n";
cin >> str1 >> str2;
int len1=str1.size(),len2=str2.size();
memset(*dp,-1,sizeof dp);
lcs_len(len1,len2);
lcs_str(len1,len2);
cout << dp[len1][len2] << endl;
cout << str << endl;
cout << "Hello world!" << endl;
return 0;
}
int lcs_len(int l1,int l2)
{
if(dp[l1][l2]!=-1)
return dp[l1][l2];
else
{
if(l1==0||l2==0)
return dp[l1][l2]=0;
if(str1[l1-1]==str2[l2-1])
return dp[l1][l2]=lcs_len(l1-1,l2-1)+1;
else
return dp[l1][l2]=max(lcs_len(l1-1,l2),lcs_len(l1,l2-1));
}
}
void lcs_str(int l1,int l2)
{
if(l1==0||l2==0)
return ;
if(dp[l1][l2]==dp[l1-1][l2])
lcs_str(l1-1,l2);
else if(dp[l1][l2]==dp[l1][l2-1])
lcs_str(l1,l2-1);
else
{
str=str1[l1-1]+str;
lcs_str(l1-1,l2-1);
}
}
边栏推荐
- offer如何选择该考虑哪些因素
- [736. LISP syntax parsing]
- Gavin teacher's perception of transformer live class - rasa project actual combat e-commerce retail customer service intelligent business dialogue robot microservice code analysis and dialogue experim
- Why is the salary of test and development so high?
- Ansible概述和模块解释(你刚走过了今天,而扑面而来的却是昨天)
- PLC模拟量输出 模拟量输出FB analog2NDA(三菱FX3U)
- STM32F103ZE+SHT30检测环境温度与湿度(IIC模拟时序)
- 【最佳网页宽度及其实现】「建议收藏」
- CentOS 7.9安装Oracle 21c历险记
- Markdown editor
猜你喜欢
Error: No named parameter with the name ‘foregroundColor‘
If you‘re running pod install manually, make sure flutter pub get is executed first.
Pointer and array are input in function to realize reverse order output
SQL injection cookie injection
Ansible报错:“msg“: “Invalid/incorrect password: Permission denied, please try again.“
Ansible概述和模块解释(你刚走过了今天,而扑面而来的却是昨天)
批量归一化(标准化)处理
带你遨游银河系的 10 种分布式数据库
AttributeError: module ‘torch._ C‘ has no attribute ‘_ cuda_ setDevice‘
A row of code r shows the table of Cox regression model
随机推荐
C语言中函数指针与指针函数
一文搞懂常见的网络I/O模型
AttributeError: module ‘torch._ C‘ has no attribute ‘_ cuda_ setDevice‘
最全常用高数公式
No experts! Growth secrets for junior and intermediate programmers and "quasi programmers" who are still practicing in Universities
第一篇论文的写作流程
全国气象数据/降雨量分布数据/太阳辐射数据/NPP净初级生产力数据/植被覆盖度数据
Redis如何实现多可用区?
装饰器基础学习02
Test interview | how much can you answer the real test interview question of an Internet company?
全链路压测:影子库与影子表之争
Why JSON is used for calls between interfaces, how fastjson is assigned, fastjson 1.2 [email protected] Mapping relatio
SQL injection HTTP header injection
Analysis -- MySQL statement execution process & MySQL architecture
《四》表单
If you‘re running pod install manually, make sure flutter pub get is executed first.
局部变量的数组初始化问题
Leetcode(417)——太平洋大西洋水流问题
window定时计划任务
U++ 元数据说明符 学习笔记