当前位置:网站首页>DP longest common subsequence detailed version (LCS)
DP longest common subsequence detailed version (LCS)
2022-07-24 09:03:00 【Don't touch ink】
Longest common subsequence , This is a B standing A blogger's explanation video , It's very good. , After watching this video, you can even stop reading my blog , But if you think the video is too long , Welcome to continue to see the essence content extracted from the video .
Catalog
1、 Definition
Longest common subsequence (LCS) It's one in a sequence set ( It's usually two sequences ) The problem of finding the longest subsequence in all sequences . A sequence of , If they are subsequences of two or more known sequences , And is the longest of all sequences that meet this condition , Is called the longest common subsequence of a known sequence .
The above explanation is given by Baidu , Let me explain it in vernacular , In fact, it means giving several different strings ( It's usually two ), Then find their common subsequence , It can be discontinuous . for instance , character string algorithms and alchemist( To facilitate observation ,LCS I use bold red to indicate ) Of LCS namely alhms.
2、 Regular questions
describe :
Given two sequences s1、s2, Find the length of the common subsequence of two rules .
Input :
On the first line, enter the first sequence
On the second line, enter the second sequence
Output :
Output a number , That is, the length of the common subsequence of two sequences .
3、 Their thinking
Dynamic programming is mainly used , But make clear the meaning .
① Design status
set up f[i][j] It's a sequence si={x1,x2,...,xi} And sequence sj={y1,y2,...,yj} The length of the common subsequence of .
② State transition equation
Now? i、j Corresponding to three different equations
f[i][j] = 0 --> i=0 perhaps j=0,si and sj The length of any sequence in is zero , So their LCS The length can only be zero
f[i][j] = f[i-1][j-1] + 1 --> xi=yj, The last characters of the two sequences are equal , Explain that the length should be increased by at least one , Add one and cut this character directly , Then continue to compare i-1 and j-1 Corresponding characters , Compare in turn
f[i][j] =max(f[i-1][j],f[i][j-1])--> xi!=yj, This is illustrated by an example , Suppose to ask for abcd and acde Of LCS length , character d!=e, Now there are two options , Either ask abcd and acd Of LCS, Either ask abc and acde Of LCS, But since it is the longest LCS, Then choose the maximum value of the two schemes .
4、 Solution code
// Find the longest common subsequence -- dp
#include <bits/stdc++.h>
using namespace std;
char s1[100],s2[100];
int f[100][100];
int main()
{
int n,m,i,j;
gets(s1);
gets(s2);
n=strlen(s1),m=strlen(s2);
// State transition equation one --> f[100][100] It was all zero , Never mind
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
if(s1[i-1]==s2[j-1]) f[i][j]=f[i-1][j-1]+1; // Fang Chenger
else f[i][j]=max(f[i-1][j],f[i][j-1]); // Equation 3
}
}
cout<<f[n][m];
return 0;
}
/* Case study
algorithms
alchemist
result : 5
*/
5、 Expand
What the above code solves is LCS The length of , Now let's try to find this sequence directly , Not the length .
// Find the longest common subsequence -- dp
#include <bits/stdc++.h>
using namespace std;
char s1[100],s2[100];
int f[100][100];
void LCS(int i,int j)
{
if(i==0||j==0) return ; // to flash back
if(s1[i-1]==s2[j-1])
{
LCS(i-1,j-1); // recursive
cout<<s1[i-1];
}
else if(f[i-1][j]>f[i][j-1])
{
LCS(i-1,j);
}
else
{
LCS(i,j-1);
}
}
int main()
{
int n,m,i,j;
gets(s1);
gets(s2);
n=strlen(s1),m=strlen(s2);
// State transition equation one --> f[100][100] It was all zero , Never mind
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
if(s1[i-1]==s2[j-1]) f[i][j]=f[i-1][j-1]+1; // Fang Chenger
else f[i][j]=max(f[i-1][j],f[i][j-1]); // Equation 3
}
}
cout<<f[n][m]<<endl; // Sequence length
LCS(n,m); // Find sequence function
return 0;
}
/* Case study
algorithms
alchemist
result :
5
alhms
*/ result :

边栏推荐
- Android系统安全 — 5.3-APK V2签名介绍
- Rank 3 and count from 0 to 9. How many times does each number appear in total, and how many times does each number appear in the tens of hundreds,
- On express framework
- Xiaobai learns oauth2
- 在npm上发布自己的库
- Protocol buffers 的问题和滥用
- JUC powerful auxiliary class
- Scatter chart - date
- Linked list - 24. Exchange nodes in the linked list in pairs
- 03_ UE4 advanced_ illumination
猜你喜欢

Run little turtle to test whether the ROS environment in the virtual machine is complete

超全总结:Go语言如何操作文件

Unity解决Package Manager“You seem to be offline”

First acquaintance with JVM

Android系统安全 — 5.2-APK V1签名介绍
![[assembly language practice] solve the unary quadratic equation ax2+bx+c=0 (including source code and process screenshots, parameters can be modified)](/img/5e/782e5c33accc455994aae044970431.png)
[assembly language practice] solve the unary quadratic equation ax2+bx+c=0 (including source code and process screenshots, parameters can be modified)

Xtrabackup realizes full backup and incremental backup of MySQL

Learn the rxjs operator

redis学习一redis介绍及NIO原理介绍

03_ UE4 advanced_ illumination
随机推荐
Android系统安全 — 5.3-APK V2签名介绍
Rk3566 add project under external
table-rowspan
在npm上发布自己的库
dp最长公共子序列详细版本(LCS)
Guys, what parameters can be set when printing flinksql so that the values can be printed? This later section is omitted. It's inconvenient. I read the configuration on the official website
【汇编语言实战】一元二次方程ax2+bx+c=0求解(含源码与过程截屏,可修改参数)
超全总结:Go语言如何操作文件
Advantages of using partitions
Un7.22: how to upload videos and pictures simultaneously with the ruoyi framework in idea and vs Code?
Taking advantage of the momentum, oceanbase promotes the lean growth of digital payment
Tiktok's "online celebrity" was poached by Amazon and broadcast on Amazon live platform
Houdini 笔记
The solution of [an error occurred while trying to create a file in the destination directory: access denied] is prompted when installing the software
JS problem summary
How to configure env.js in multiple environments in uni app
After two days of tossing and turning, I finally wrote my first fluent app, which almost tortured me into a nervous breakdown
Unity C tool class arrayhelper
Publish your own library on NPM
脉脉网友出了道 Go 面试题,你能答对吗?