当前位置:网站首页>[solution] longest common subsequence
[solution] longest common subsequence
2022-06-29 01:20:00 【wingaso】
subject
Title Description
Find two strings that contain only lowercase letters X , Y X,Y X,Y The longest common subsequence length of .
Input format
The first line is two positive integers m , n ( m < 1000 , n < 1000 ) m,n(m < 1000,n < 1000) m,n(m<1000,n<1000), There are two X , Y X,Y X,Y The length of two strings .
The second line is a length of m m m A string containing only lowercase letters .
The third line is a n n n A string containing only lowercase letters .
Output format
Output a positive integer according to the requirements of the topic .
Answer key
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
const int N = 1010;
int f[N][N];
char X[N],Y[N];
int main(){
int m,n;
cin >> m >> n;
cin >> X + 1 >> Y + 1;
for(int i = 1;i <= m;i++)
for(int j = 1;j <= n;j++)
if(X[i] == Y[j]) f[i][j] = f[i - 1][j - 1] + 1;
else f[i][j] = max(f[i][j - 1],f[i - 1][j]);
cout << f[m][n] << endl;
return 0;
}
This problem is an introduction to dynamic programming .
Please make sure you understand what the sequence is before you understand this problem .
for instance :
For strings abcdefgh,abc、bcd It's his subsequence ,acfg、dgh It is also its subsequence .
Elements in a subsequence can be discontinuous in the original sequence .
Be similar to Fibonacci sequence This question , This problem also uses an array f f f Store something we need state .
f [ i ] [ j ] f[i][j] f[i][j] Express X X X Before i i i Characters and Y Y Y Before j j j The longest common subsequence of characters .
—— Don't ask why you can say so , That's right .
Let's assume that it can be expressed in this way , that f [ m ] [ n ] f[m][n] f[m][n] This is the value we want to output .
So let's see f [ i ] [ j ] f[i][j] f[i][j] How exactly can it be derived .
Similar to Fibonacci f [ i ] = f [ i − 1 ] + f [ i − 2 ] f[i] = f[i - 1] + f[i - 2] f[i]=f[i−1]+f[i−2], our f [ i ] [ j ] f[i][j] f[i][j] Can we also draw from the smaller data scale ?
The answer is yes .
f [ i ] [ j ] = { f [ i − 1 ] [ j − 1 ] + 1 , X [ i ] = Y [ j ] m a x ( f [ i − 1 ] [ j ] , f [ i ] [ j − 1 ] ) , X [ i ] ≠ Y [ i ] f[i][j] = \left\{\begin{matrix} f[i - 1][j - 1] + 1,X[i] = Y[j]\\ max(f[i - 1][j],f[i][j - 1]),X[i] \neq Y[i] \end{matrix}\right. f[i][j]={ f[i−1][j−1]+1,X[i]=Y[j]max(f[i−1][j],f[i][j−1]),X[i]=Y[i]
To be continued ...
Originality is not easy. , Thank you for your support !
边栏推荐
- [MCU club] design of classroom number detection based on MCU [simulation design]
- How to select database
- [MCU club] design of classroom number detection based on MCU [physical design]
- 手把手教你搞懂测试环境项目部署
- 统计字符串中不同回文子序列的个数
- Different subsequence problems I
- Advanced installer architect authoring tool
- Qt est basé sur le système de gestion RFID (peut être appliqué à la plupart des systèmes de gestion RFID)
- linux7(centos7)设置oracle11开机自启动
- 分析框架——用户体验度量数据体系搭建
猜你喜欢

Browser cache library design summary (localstorage/indexeddb)

4276. 擅长C

Linux7 (centos7) setting oracle11 boot auto start

What is the difference between the histology and western blotting 两种方法都是进行组织染色的

Installing Oracle database in docker

Pytorch -- use and modification of existing network model

盘点 6 月 yyds 的开源项目!

Statistical learning method (2/22) perceptron

How to solve the problem of Caton screen when easycvr plays video?

Depth first search to realize the problem of catching cattle
随机推荐
Breadth first search to catch cattle
Basic use of flask Sqlalchemy
[eight part essay] MySQL
Code repetition of reinforcement learning based parameters adaptation method for particlewarn optimization
XML and other file contents in idea cannot be highlighted, and the file becomes gray
第七天 脚本与特效
分享自己平时使用的socket多客户端通信的代码技术点和软件使用
Statistical learning method (2/22) perceptron
Sword finger offer 16 Integer power of numeric value
Exclusive analysis | about resume and interview
What is the difference between immunohistochemistry and immunohistochemistry?
How to solve the problem of Caton screen when easycvr plays video?
最大路径和问题(摘樱桃问题)
What is the reason for the service crash caused by replacing the easycvr cluster version with the old database?
SRAM和DRAM之间的异同
be based on. NETCORE development blog project starblog - (13) add friendship link function
基于.NetCore开发博客项目 StarBlog - (13) 加入友情链接功能
有了这款工具,自动化识别验证码再也不是问题
[MCU club] design of blind water cup based on MCU [physical design]
Browser cache library design summary (localstorage/indexeddb)