当前位置:网站首页>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);
}
}
边栏推荐
- JS variable plus
- 【Android Kotlin协程】利用CoroutineContext实现网络请求失败后重试逻辑
- PMP证书有没有必要续期?
- 最全常用高数公式
- Error: No named parameter with the name ‘foregroundColor‘
- U++ game learning notes
- 为什么很多人对技术债务产生误解
- Ansible overview and module explanation (you just passed today, but yesterday came to your face)
- 谈谈讲清楚这件事的重要性
- Appium practice | make the test faster, more stable and more reliable (I): slice test
猜你喜欢
No experts! Growth secrets for junior and intermediate programmers and "quasi programmers" who are still practicing in Universities
Techniques d'utilisation de sublime
Flask项目使用flask-socketio异常:TypeError: function() argument 1 must be code, not str
3GPP信道模型路损基础知识
Common Oracle SQL statements
《二》标签
Ansible中的inventory主機清單(預祝你我有數不盡的鮮花和浪漫)
Markdown编辑器
How to design API interface and realize unified format return?
[736. LISP syntax parsing]
随机推荐
【QT】自定义控件-Loading
app内嵌h5---iphone软键盘遮挡输入文字
PMP证书有没有必要续期?
【Android Kotlin协程】利用CoroutineContext实现网络请求失败后重试逻辑
npm ERR! 400 Bad Request - PUT xxx - “devDependencies“ dep “xx“ is not a valid dependency name
Ansible reports an error: "MSG": "invalid/incorrect password: permission denied, please try again“
《四》表单
HarmonyOS第四次培训
01 machine learning related regulations
《二》标签
If you‘re running pod install manually, make sure flutter pub get is executed first.
3.基金的类型
How to design API interface and realize unified format return?
IMS data channel concept of 5g vonr+
JS variable
Auto.js 获取手机所有app名字
qt 简单布局 盒子模型 加弹簧
《五》表格
App embedded H5 --- iPhone soft keyboard blocks input text
【最佳网页宽度及其实现】「建议收藏」