当前位置:网站首页>Longest common subsequence (record path version)
Longest common subsequence (record path version)
2022-04-23 01:43:00 【Stick to your original heart and go to your dream】
Record path
Since we mentioned last time that we can pass ** Memory search as well as Dynamic programming and tabulation ** Get its length , But the curious thing is , Can we use some simple means , Record its path ??
The answer, of course, is Sure Of , We just need one An array of the same size Can satisfy our little wish .
Action is better than action ~~~ Please look at the code. :
Memory search
int process(string& str1, string& str2, int i, int j) {
// With i,j Subscript for the end
if (dp[i][j])// If the answer to this position has been solved , Then return directly to the answer
{
return dp[i][j];
}
if (i == 0 && j == 0) {
// Analyze various boundary conditions ( When one or both strings have only one character left , Analyze the equality and inequality of the two ), Find recursive exit
if (str1[i] == str2[j]) {
dp[i][j] = 1;
}
flag[i][j] = 1;// Record path ( If they are the same, mark them as 1, Because there is only one character left in both strings ,
// So whether it's marked as 1,2, still 3( That is, wherever you go ) It's going to end , So it's not important to mark the number here )
return dp[i][j];
}
else if (i == 0) {
if (str1[i] == str2[j]) {
dp[i][j] = 1;
flag[i][j] = 1;
return dp[i][j];
}
else {
// If different , Then marked as 2( Go to the left )
dp[i][j] = process(str1, str2, i, j - 1); flag[i][j] = 2; return dp[i][j];
}
}
else if (j == 0) {
if (str1[i] == str2[j]) {
flag[i][j] = 1;
dp[i][j] = 1; return dp[i][j];
}
else {
// Marked as 3( Go to the right )
dp[i][j] = process(str1, str2, i - 1, j); flag[i][j] = 3; return dp[i][j];
}
}
int p1 = process(str1, str2, i, j - 1);
int p2 = process(str1, str2, i - 1, j); int p3 = 0;
if (str1[i] == str2[j]) {
p3 = process(str1, str2, i - 1, j - 1) + 1;
flag[i][j] = 1;// If they are equal, mark
}
dp[i][j] = max(max(p1, p2), p3);
if (!flag[i][j]) {
// If it works here , That means it wasn't marked before , That is, the two characters are not equal , Then find the mark with the largest length in the next step
if (p2 >= p1)flag[i][j] = 3;
else flag[i][j] = 2;
}
return dp[i][j];
}
So let's say , We just need to define one flag The array can record the path , So how to output ? Please have a look at FindPath function
void FindPath(int i, int j, string& text1, string& text2) {
if (i < 0 || j < 0)return;
if (flag[i][j] == 1) {
FindPath(i - 1, j - 1, text1, text2);
cout << text1[i];//text2[j]
}
else if (flag[i][j] == 2)
{
FindPath(i, j - 1, text1, text2);
}
else
{
FindPath(i - 1, j, text1, text2);
}
}
The main function says :
int longestCommonSubsequence(string text1, string text2) {
dp.resize(text1.size(), vector<int>(text2.size())); flag.resize(text1.size(), vector<int>(text2.size()));
string path = "";
int ana = process(text1, text2, text1.size() - 1, text2.size() - 1);
FindPath(text1.size() - 1, text2.size() - 1, text1, text2);
return ana;
}
int main() {
cout << longestCommonSubsequence("YiQingTuiSan", "YIQINGTUISAN") << endl;
for (vector<int>t : flag) {
for (int i : t)cout << i << " ";
cout << endl;
}
return 0;
}

see ! That's the charm of recursion , We just need to define the conditions , Leave the rest to recursion, hahaha
Cough , Return to the right topic …
Now that we know how to write a memory search version , Then dynamic programming is easy to catch …
Dynamic programming
Note that there i And j from 1 Start , Because we put the boundary of the above memorized search into the loop , So add one more layer on both sides
void LCS()
{
for(int i = 1; i <= len1; i++){
for(int j = 1; j <= len2; j++){
if(s1[i-1] == s2[j-1]){
// notes : Here s1 And s2 The sequence is from s1[0] And s2[0] At the beginning
c[i][j] = c[i-1][j-1] + 1;
b[i][j] = 1;// Mark each situation , It is convenient to enter the path later
}
else{
//c[i][j] = max(c[i][j-1], c[i-1][j])
if(c[i][j-1] >= c[i-1][j]){
c[i][j] = c[i][j-1];
b[i][j] = 2; // The second case
}
else{
c[i][j] = c[i-1][j];
b[i][j] =3; // The third case
}
}
}
}
}
So should our output function also adapt to local conditions ?
void FindPath(int i, int j, string& text1, string& text2) {
if (i == 0 || j == 0)return;// It's different here , Remember the length of the upper pass
if (flag[i][j] == 1) {
FindPath(i - 1, j - 1, text1, text2);
cout << text1[i];//text2[j]
}
else if (flag[i][j] == 2)
{
FindPath(i, j - 1, text1, text2);
}
else
{
FindPath(i - 1, j, text1, text2);
}
}
版权声明
本文为[Stick to your original heart and go to your dream]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230138051533.html
边栏推荐
- Slow response of analysis API
- 无关联进程间通信 —— 命名管道的创建和使用
- UVC camera encapsulation class
- Introduction to gbase 8s storage structure and space management
- GBase8s SQL 引擎框架简介
- Introduction to gbase 8s checkpoint
- mb_substr()、mb_strpos()函数(故事篇)
- 2022第六季完美童模 IPA國民賽領跑元宇宙賽道
- 科技云报道:云计算进入“下半场”,国产云的出路在哪儿?
- Self taught programming, don't read theory books foolishly, programmer: it's all left over by others
猜你喜欢

W801 / w800 WiFi socket development (I) - UDP

Futr3d: a unified 3D detection framework for sensor fusion

Chapter 6 uses Matplotlib to draw thermodynamic diagram

DFS奇偶性剪枝

ai2022新功能,illustrator 2022 新功能介绍

W801/W800-wifi-socket开发(一)-UDP

NR polar code 七- SCL(succesive cancellation list decoding)

Digital collection platform settled in digital collection platform to develop SaaS platform of digital collection

第六章 使用 matplotlib 绘制热力图
![[经验教程]支付宝余额自动转入余额宝怎么设置关闭取消支付宝余额自动转入余额宝?](/img/d5/6aa14af59144b8c99aa6a367479f6b.png)
[经验教程]支付宝余额自动转入余额宝怎么设置关闭取消支付宝余额自动转入余额宝?
随机推荐
NR polar code 七- SCL(succesive cancellation list decoding)
稳定币是让公链加速死亡或者取代BTC的超级机会?
Itextsharp infrastructure
Itextsharp page setup
Unity combines itextsharp to generate PDF preparation DLL
Find number (DFS)
Glide set fillet image (support custom fillet position)
Vscode + PHP debug + namespace guidelines
42. Use k3det in mmrotate for rotating target detection, MNN deployment and ncnn deployment
蒜头君开公司(DFS全排列)
Counting garlic customers: Sudoku (DFS)
K zeros after leetcode factorial function
Oracle database query lock table SQL script and delete lock information script (necessary for database development ETL and DBA)
角色個人屬性英文縮寫
LSF的常用使用命令总结
第六章 使用 matplotlib 绘制热力图
Is the stable currency a super opportunity to accelerate the death of the public chain or replace BTC?
《维C中国》乡村助农暖人心第三站嘉宝果农场
Gbase 8s group by function introduction
GBASE 8s分片表管理操作