当前位置:网站首页>[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 !
边栏推荐
- 狼人杀休闲游戏微信小程序模板源码/微信小游戏源码
- What is the difference between the history and Western blotting
- Cocoscrreator dynamically switches skeletondata to update bones
- Pytorch -- use and modification of existing network model
- 手把手教你搞懂测试环境项目部署
- GUI Graphical user interface programming example - color selection box
- Rasa dialogue robot helpdesk (V)
- 测试只能干到35岁?35岁+的测试就会失业?
- The latest justnews theme source code 6.0.1 happy version + social Q & a plug-in 2.3.1+ tutorial
- Docker中安裝Oracle數據庫
猜你喜欢

免疫组化和免疫组学之间的区别是啥?

TypeScript(6)函数

TypeScript(4)接口

Depth first search to realize the problem of catching cattle

Teach you how to understand the test environment project deployment

EasyCVR服务private.pem文件被清空,导致无法正常启动该如何处理?

Blazor University (34) forms - get form status

DO280分配持久性存储

最新版CorelDRAW Technical Suite2022

Esmm reading notes
随机推荐
第八天 脚本与音频
FSS object storage how to access the Intranet
Day 8 script and audio
TypeScript(7)泛型
Uvm:field automation mechanism
【图像处理】基于matlab实现图像曲线调整系统
最大路径和问题(摘樱桃问题)
[MCU club] design of classroom number detection based on MCU [physical design]
Browser cache library design summary (localstorage/indexeddb)
802.1x协议简述
3D, point cloud splicing
NOIP2006-2018 提高组 初赛试题完善程序题 CSP-S 2019 2020 初赛试题完善程序题
狼人杀休闲游戏微信小程序模板源码/微信小游戏源码
Depth first search to realize the problem of catching cattle
cocoscreator动态切换SkeletonData实现骨骼更新
What kind of life is a tester with a monthly salary of over 10000?
What is the difference between the history and Western blotting
统计字符串中不同回文子序列的个数
What is contemporaneous group analysis? Teach you to use SQL to handle
WPF 实现心电图曲线绘制