当前位置:网站首页>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 :

边栏推荐
- 【FFH】OpenHarmony啃论文成长计划---cJSON在传统C/S模型下的应用
- After two days of tossing and turning, I finally wrote my first fluent app, which almost tortured me into a nervous breakdown
- JS built-in method
- Xiaobai learns Jenkins - installation and quick start
- 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,
- 4、 Midway integrates swagger and supports JWT bearers
- Android系统安全 — 5.3-APK V2签名介绍
- 面试官:哥们Go语言的读写锁了解多少?
- Android system security - 5.3-apk V2 signature introduction
- TCP triple handshake connection combing
猜你喜欢
随机推荐
One click openstack single point mode environment deployment - preliminary construction
[emotion] what is "excellent"
链表——19. 删除链表的倒数第 N 个结点
Tiktok shop will add a new site, and the Singapore site will be launched on June 9
Houdini official HDA sidefx labs installation
Tiktok live broadcast with goods marketing play
TT ecosystem - cross border in-depth selection
Android system security - 5.2-apk V1 signature introduction
Practice 4-6 number guessing game (15 points)
Description of MATLAB functions
Let's test 5million pieces of data. How to use index acceleration reasonably?
【我的创作一周年纪念日】爱情是需要被纪念的,创作也是
Advantages of using partitions
03_ UE4 advanced_ illumination
Online lover
[Shangshui Shuo series] final rad New Literacies
[FFH] openharmony gnawing paper growth plan -- Application of cjson in traditional c/s model
Unity C#工具类 ArrayHelper
Sword finger offer II 024. reverse linked list
[assembly language practice] solve the unary quadratic equation ax2+bx+c=0 (including source code and process screenshots, parameters can be modified)









