当前位置:网站首页>最长公共子序列(一)(动规)
最长公共子序列(一)(动规)
2022-04-21 12:17:00 【4nc414g0n】
最长公共子序列(一)
题目链接
题目描述:
给定两个字符串 s1 和 s2,长度为m和n 。求两个字符串最长公共子序列的长度。
所谓子序列,指一个字符串删掉部分字符(也可以不删)形成的字符串。例如:字符串 “arcaea” 的子序列有 “ara” “rcaa” 等。但 “car” “aaae” 则不是它的子序列。
所谓 s1 和 s2 的最长公共子序列,即一个最长的字符串,它既是 s1 的子序列,也是 s2 的子序列。
数据范围 : 0\leq m,n\leq 10000≤m,n≤1000 。保证字符串中的字符只有小写字母。
要求:空间复杂度 O(mn)O(mn),时间复杂度 O(mn)O(mn)
进阶:空间复杂度 O(min(m,n))O(min(m,n)),时间复杂度 O(mn)O(mn)
输入:
"abcde","bdcaaa"
输出:
2
思路:
s1:bcdes2:bdc- 状态方程:
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
代码如下:class Solution { public: int LCS(string s1, string s2) { vector<vector<int>>dp(s1.size()+1,vector<int>(s2.size()+1,0)); int n1=s1.size(),n2=s2.size(); for(int i=1;i<=n1;i++) { for(int j=1;j<=n2;j++) if(s1[i-1]!=s2[j-1]) dp[i][j]=max(dp[i-1][j],dp[i][j-1]); else dp[i][j]=dp[i-1][j-1]+1; } return dp[n1][n2]; } }; ``
版权声明
本文为[4nc414g0n]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_41420788/article/details/124197034
边栏推荐
- 虚拟货币已然没有市场,为何还能对一些人产生不小的诱惑?
- Cultdao, a decentralized VC platform, launched a vote on the betamars project, which is currently in progress
- [Software Testing Series II] software testing process specification
- [Software Testing Series IV] "test points to be paid attention to in software testing"
- 集成公告|KeyFi现已上线Moonbeam
- 达梦数据库市场份额增速行业领先,盈利能力大幅提升
- 小程序旋转手机推流,远端拉流画面被裁剪的问题
- Three. JS learning project -- 3D data visualization of anti US aggression and aid Korea
- One day, Alibaba database will squeeze Oracle out of the market
- 【软件测试系列三】《测试用例编写原则与设计方法》
猜你喜欢

Aaai2022 | unbiased temporal knowledge reasoning based on probabilistic soft logic

《深度学习》学习笔记(七)

顶流“健身博主”刘畊宏

Three. JS learning project -- 3D data visualization of anti US aggression and aid Korea
![[200 opencv routines by youcans] 159 Fixed threshold method for image processing](/img/ae/78fe6543589d0d0f5450c5ca0ae067.png)
[200 opencv routines by youcans] 159 Fixed threshold method for image processing

China Resources Yibao is rumored to have an IPO, and nongnongshanquan may usher in an early "old enemy"

How nodejs converts buffer data to string

Chinese character extraction operation of Sao package (string, no re, in)

CVPR 2022 oral | Hong Kong Chinese open source posec3d: skeleton motion recognition framework based on 3d-cnn

Integration announcement keyfi is now online moonbeam
随机推荐
S参数简介
Scala installation and development environment configuration tutorial
[Software Testing Series III] "test case writing principles and design methods"
Berkeley, Samsung | a fast post training converter pruning framework
Musk lives in the old Internet age?
[software test series IX] description of matters to be provided for pressure test application
WordPress aministrazione aperta 3.7.3 arbitrary file reading
【软件测试系列三】《测试用例编写原则与设计方法》
直播江湖生变:李佳琦被困豪宅,罗永浩淡出,薇娅雪梨助播上位
[data visualization application] draw bivariate mapping map (with R language code)
Is it difficult to choose binary version control tools? After reading this article, you will find the answer
【软件测试系列十一】《压力测试场景及测试用例模板》
[BSidesCF 2019]Kookie
游戏行业实战案例2:玩家等级
[software test series Xi] stress test scenario and test case template
Oracle 通过 Exadata 云基础设施 X9M 提供卓越的数据库性能和规模
中介包围小红书
Aicoco AI frontier promotion (4.21)
Notepad + + how to copy multiple lines and paste them to the corresponding position
AAAI2022|基于概率软逻辑的无偏时序常识知识推理
