当前位置:网站首页>动态规划之线性dp(下)
动态规划之线性dp(下)
2022-07-31 15:59:00 【std i hurt o love】
一、 最长上升子序列(一)
描述
给定一个长度为 n 的数组 arr,求它的最长严格上升子序列的长度。
所谓子序列,指一个数组删掉一些数(也可以不删)之后,形成的新数组。例如 [1,5,3,7,3] 数组,其子序列有:[1,3,3]、[7] 等。但 [1,6]、[1,3,5] 则不是它的子序列。
我们定义一个序列是 严格上升 的,当且仅当该序列不存在两个下标 i和 j 满足 i<j且 arri≥arrj。
数据范围:0≤n≤1000 ,|arri∣≤10 ^9
要求:时间复杂度 O(n^2), 空间复杂度 O(n)
输入描述:
第一行输入一个正整数 n ,表示数组的长度
第二行输入 n 个整数表示数组的每个元素。
输出描述:
输出最长严格上升子序列的长度
示例1
输入:
7
6 3 1 5 2 3 7
复制
输出:
4
复制
说明:
该数组最长上升子序列为 [1,2,3,7] ,长度为4
解法:
这是一道动态规划题目。将其分解为子问题,即分解成求解n个 以0到n-1下标的数为结尾的子序列的最大长度的问题。
可以用一个dp数组保存每个数做结尾时对应的最大长度。
假设一个以第i个元素arr[i]结尾的子序列Si,那么Si的长度是由i在Si中的前一个元素arr[j]决定的,Si的长度就等于Sj的长度加一。
这前面一个数arr[j]必须要满足两个条件:
arr[j]<arr[i],这样才能构成严格上升
dp[j]的值在i之前的所有dp中最大,这样我们求的才是最大长度。
#include<iostream>
#include<vector>
using namespace std;
int main(){
int n;
cin>>n;
vector<int> arr(n);
for(int i=0;i<n;i++)
cin>>arr[i];
vector<int> dp(n,1);//以i下标的数为结尾的子序列的长度
for(int i=1;i<n;i++){
int mx=0;
for(int j=0;j<i;j++){
//每一个元素的dp值==在它之前的,比它小的元素中最大的dp值加一
if(arr[i]>arr[j])
mx=max(mx,dp[j]);
}
dp[i]=mx+1;
}
int res=1;
for(int i=0;i<n;i++)
res=max(res,dp[i]);
cout<<res;
return 0;
}
时间复杂度:O(n^2)
空间复杂度:O(n)
二、最长公共子序列(一)
#include<iostream>
#include<vector>
#include<string>
using namespace std;
class Solution {
public:
int longestSeqNum(string &s, string &t){
int m = s.size();
int n = t.size();
vector<vector<uint64_t>>dp(m+1,vector<uint64_t>(n+1,0));// dp[i][j] 表示 i-1结尾的s j-1结尾 t的
// 最长公共 子序列的长度
// 注意要弄懂 dp[i][j] 的含义 不要有 dp[0][0] = 1; 这什么鬼
for (int i =1;i<=m;i++){
for(int j =1;j<= n;j++){
if(s[i-1] ==t[j-1]){
dp[i][j] = dp[i-1][j-1] +1;
}else {
dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
}
}
}
return dp[m][n];
}
};
int main(){
int n,m;
string s,t;
// 题目输入格式
cin >> n >> m;
cin >> s>> t;
Solution solu;
cout<< solu.longestSeqNum(s,t) <<endl;
return 0;
}
边栏推荐
- The principle of hough transform detection of straight lines (opencv hough straight line detection)
- 【7.28】代码源 - 【Fence Painting】【合适数对(数据加强版)】
- How C programs run 01 - the composition of ordinary executable files
- Oracle动态注册非1521端口
- Browser's built-in color picker
- 多主复制的适用场景(1)-多IDC
- 多主复制下处理写冲突(4)-多主复制拓扑
- ML.NET related resources
- Matlab matrix basic operations (definition, operation)
- 【7.29】Code Source - 【Arrangement】【Stone Game II】【Cow and Snacks】【Minimum Number of Spawns】【Sequence】
猜你喜欢

苹果官网样式调整 结账时产品图片“巨大化”

基于Redis(SETNX)实现分布式锁,案例:解决高并发下的订单超卖,秒杀

Qt practical cases (54) - using transparency QPixmap design pictures

The 2nd China PWA Developer Day

【C语言】LeetCode27.移除元素

LevelSequence源码分析

Dialogue with Zhuang Biaowei: The first lesson of open source

【7.29】Code Source - 【Arrangement】【Stone Game II】【Cow and Snacks】【Minimum Number of Spawns】【Sequence】

The use of border controls

T - sne + data visualization parts of the network parameters
随机推荐
The normal form of the database (first normal form, second normal form, third normal form, BCNF normal form) "recommended collection"
【7.29】代码源 - 【排列】【石子游戏 II】【Cow and Snacks】【最小生成数】【数列】
C语言”三子棋“升级版(模式选择+AI下棋)
字符串反转的实现方法总结「建议收藏」
mysql black window ~ build database and build table
"Autumn Recruitment Series" MySQL Interview Core 25 Questions (with answers)
After Grafana is installed, the web opens and reports an error
基于ABP实现DDD
C language - function
Emmet syntax
[TypeScript] In-depth study of TypeScript type operations
i.MX6ULL driver development | 33 - NXP original network device driver reading (LAN8720 PHY)
网站漏洞修复服务商关于越权漏洞分析
How Redis handles concurrent access
Visualize GraphQL schemas with GraphiQL
tensorflow2.0 cnn(layerwise)
Matlab matrix basic operations (definition, operation)
C语言-函数
What is the difference between BI software in the domestic market?
MySQL基础篇【单行函数】