当前位置:网站首页>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);
}
}边栏推荐
- qt 简单布局 盒子模型 加弹簧
- 最长公共子序列(LCS)(动态规划,递归)
- 【QT】自定义控件-Loading
- AttributeError: module ‘torch._C‘ has no attribute ‘_cuda_setDevice‘
- 2. Overview of securities investment funds
- 全国气象数据/降雨量分布数据/太阳辐射数据/NPP净初级生产力数据/植被覆盖度数据
- 01 machine learning related regulations
- Liste des hôtes d'inventaire dans ansible (je vous souhaite des fleurs et de la romance sans fin)
- 【ArcGIS教程】专题图制作-人口密度分布图——人口密度分析
- torch optimizer小解析
猜你喜欢

全链路压测:影子库与影子表之争

Meow, come, come: do you really know if, if else

Time complexity & space complexity

装饰器基础学习02

《五》表格

【opencv】图像形态学操作-opencv标记不同连通域的位置

Techniques d'utilisation de sublime
[email protected]映射关系问题"/>接口间调用为什么要用json、fastjson怎么赋值的、fastjson [email protected]映射关系问题

Dynamically generate tables

Sublime tips
随机推荐
Understand common network i/o models
If you‘re running pod install manually, make sure flutter pub get is executed first.
U++ 游戏类 学习笔记
与利润无关的背包问题(深度优先搜索)
Error: No named parameter with the name ‘foregroundColor‘
STM32封装ESP8266一键配置函数:实现实现AP模式和STA模式切换、服务器与客户端创建
一个酷酷的“幽灵”控制台工具
记录一次压测经验总结
一文搞懂常见的网络I/O模型
Two methods of chromosome coordinate sequencing
Talk about the importance of making it clear
当 Knative 遇见 WebAssembly
JS also exports Excel
vector和类拷贝构造函数
最长回文子串(动态规划)
Mysql database (basic)
ASP. Net MVC - resource cannot be found error - asp Net MVC – Resource Cannot be found error
Why is the salary of test and development so high?
《二》标签
Liste des hôtes d'inventaire dans ansible (je vous souhaite des fleurs et de la romance sans fin)