当前位置:网站首页>[dynamic programming]dp19 longest common subsequence (I) - medium
[dynamic programming]dp19 longest common subsequence (I) - medium
2022-07-18 17:51:00 【51CTO】
DP19 Longest common subsequence ( One )
describe
Given two strings s1 and s2, The length is n and m . Find the length of the longest common subsequence of two strings .
So called subsequences , Refers to the deletion of some characters from a string ( It's also possible to delete ) Formed string . for example : character string "arcaea" The subsequences of are "ara" 、 "rcaa" etc. . but "car" 、 "aaae" Is not its subsequence .
So-called s1 and s2 The longest common subsequence of , That is, the longest string , It is both s1 The subsequence , It's also s2 The subsequence .
Data range : . Only lowercase characters in the string are guaranteed .
requirement : Spatial complexity , Time complexity
Advanced : Spatial complexity , Time complexity
Input description :
Enter an integer in the first line n and m , Representation string s1 and s2 The length of .
Next, enter a string in the second and third lines respectively s1 and s2.
Output description :
Output the length of the longest common subsequence of two strings
Example 1
Input :
Copy
Output :
Copy
explain :
Example 2
Input :
Copy
Output :
Answer key
Dynamic programming solution
step :
- Definition dp[i][k] Representation string a And string b The lengths are i and k When , The longest substring length
- initialization :dp[0][k] = 0,dp[i][0] = 0, Indicates that a string is 0 When , The length of the longest matching substring with another string is 0
- The recursive formula :
- If a[i-1] be equal to b[k-1], Indicates that the current character matches ,dp[i][k] = dp[i-1][k-1]+1
- otherwise ,dp[i][k] be equal to max(dp[i][k-1],dp[i-1][k])
The code is as follows :
边栏推荐
- MFC problem solving process appmanage
- Qt Creator调试模式断点不起作用 MinCW可以
- (PC+WAP)织梦模板公司行业通用类网站
- 【CVA估值训练营】如何快速读懂上市公司年报(第一讲)
- MySQL master / master-slave replication /xtrabackup/binlog database recovery and common modules using ansible
- Principle and application of vulnerability scanning
- QT creator debug mode breakpoint does not work mincw can
- [paper notes] - face recognition facenet - 2015-cvpr
- HCIA-R&S自用笔记(6)(网络层)ICMP、IP协议基础及分片
- 如何解决8080端口被占用
猜你喜欢

Shengxin weekly issue 36

2022年山东省安全员C证考试题库及答案

平衡二叉树

Setting method of win11 filtering error log

SWD/JTAG Communication Failure和No Target Connected

五 其它目标和通用选项的介绍
![[paper reading] multi task attention based semi supervised learning for medical image segmentation](/img/5d/52cc0fb857d088761e4232c6867ffb.png)
[paper reading] multi task attention based semi supervised learning for medical image segmentation

机械臂速成小指南(零点五):机械臂相关资源

Quick completion guide of manipulator (zero five): resources related to manipulator

2022年起重机械指挥考试模拟100题模拟考试平台操作
随机推荐
Tcp/ip protocol of network principle
五 其它目标和通用选项的介绍
The United States claimed that it did not "block" Russian agricultural products. Russian lawmakers criticized the United States as the "initiator" of the crisis
新书上市 | C 语言经典教材配套“习题解答”,原书累计印数 10 万 +
什么是丢包,为什么会丢包
Py之lime:lime库的简介、安装、使用方法之详细攻略
PostgreSQL source code (8) xlog initialization
slab为什么要进行染色处理
守护线程及应用场景
Markdown分界线——CSDN画分界线
Flink1.7 from installation to experience
Data Lake (11): Iceberg table data organization and query
Ericsson asked for prohibition on the grounds of infringement, and apple returned to the United States to counterclaim. This scene is so familiar
2022年山东省安全员C证考试题库及答案
V831——AprilTag标签识别
The first China Digital Collection conference was successfully held
Diabetes genetic risk testing challenge -toggle 30 days of ML
One week's wonderful content sharing (issue 12) repetition
HCIA-R&S自用笔记(9)数据转发过程、单播/多播/组播
JS Base64 to picture