当前位置:网站首页>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);
}
}
边栏推荐
- npm ERR! 400 Bad Request - PUT xxx - “devDependencies“ dep “xx“ is not a valid dependency name
- 01机器学习相关规定
- C语言中函数指针与指针函数
- STM32 system timer flashing LED
- 最长公共子序列(LCS)(动态规划,递归)
- Appium practice | make the test faster, more stable and more reliable (I): slice test
- ASP. Net MVC - resource cannot be found error - asp Net MVC – Resource Cannot be found error
- Markdown editor
- 与利润无关的背包问题(深度优先搜索)
- Monitoring cannot be started after Oracle modifies the computer name
猜你喜欢
Time complexity & space complexity
Auto.js 获取手机所有app名字
JS variable plus
U++ 元数据说明符 学习笔记
Pointer and array are input in function to realize reverse order output
Error: No named parameter with the name ‘foregroundColor‘
No experts! Growth secrets for junior and intermediate programmers and "quasi programmers" who are still practicing in Universities
Ansible中的inventory主机清单(预祝你我有数不尽的鲜花和浪漫)
pmp真的有用吗?
01 machine learning related regulations
随机推荐
Dynamically generate tables
Redis如何实现多可用区?
U++ game learning notes
SQL injection HTTP header injection
JS input and output
SQL injection cookie injection
Markdown编辑器
Two methods of chromosome coordinate sequencing
全链路压测:影子库与影子表之争
ClickHouse(03)ClickHouse怎么安装和部署
Development thoughts of adding new requirements in secondary development
局部变量的数组初始化问题
STM32F103 realize IAP online upgrade application
3.基金的类型
CentOS 7.9安装Oracle 21c历险记
Sublime tips
The execution order of return in JS' try catch finally
Comparison between thread and runnable in creating threads
2039: [蓝桥杯2022初赛] 李白打酒加强版 (动态规划)
想要选择一些部门优先使用 OKR, 应该如何选择试点部门?