当前位置:网站首页>【动态规划】—— 线性DP
【动态规划】—— 线性DP
2022-06-29 09:35:00 【玄澈_】

例题:AcWing 898. 数字三角形


AC代码
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 510, INF = 1e9;
int n;
int a[N][N];
int f[N][N];
int main()
{
cin >> n;
for(int i = 1; i <= n; i ++ )
for(int j = 1; j <= i; j ++ )
scanf("%d", &a[i][j]);
for(int i = 0; i <= n; i ++ )
for(int j = 0; j <= n; j ++ )
f[i][j] = -INF;
f[1][1] = a[1][1];
for(int i = 2; i <= n; i ++ )
for(int j = 1; j <= i; j ++ )
f[i][j] = max(f[i - 1][j], f[i - 1][j - 1]) + a[i][j];
int res = -INF;
for(int i = 1; i <= n; i ++ ) res = max(res, f[n][i]);
cout << res << endl;
return 0;
}例题:AcWing 895. 最长上升子序列 (LIS)



朴素做法的DP公式:
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1010;
int n;
int a[N], f[N];
int main()
{
cin >> n;
for(int i = 1; i <= n; i ++ ) scanf("%d", &a[i]);
for(int i = 1; i <= n; i ++ )
{
f[i] = 1; // 只有a[i]一个数的情况
for(int j = 1; j <= i - 1; j ++ )
if(a[j] < a[i])
f[i] = max(f[i], f[j] + 1);
}
int res = 0;
for(int i = 1; i <= n; i ++ ) res = max(res, f[i]);
cout << res << endl;
return 0;
}记录最长上升子序列 (逆序)
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1010;
int n;
int a[N], f[N];
int g[N];
int main()
{
cin >> n;
for(int i = 1; i <= n; i ++ ) scanf("%d", &a[i]);
for(int i = 1; i <= n; i ++ )
{
f[i] = 1; // 只有a[i]一个数的情况
for(int j = 1; j <= i - 1; j ++ )
if(a[j] < a[i])
if(f[i] < f[j] + 1)
{
f[i] = f[j] + 1;
g[i] = j;
}
}
int k = 1;
for(int i = 1; i <= n; i ++ )
if(f[k] < f[i])
k = i;
for(int i = 0, len = f[k]; i < len; i ++ )
{
cout << a[k] << ' ';
k = g[k];
}
return 0;
}AcWing 897. 最长公共子序列


#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1010;
int n, m;
char a[N], b[N];
int f[N][N];
int main()
{
cin >> n >> m;
scanf("%s%s",a + 1, b + 1);
for(int i = 1; i <= n; i ++ )
for(int j = 1; j <= m; j ++ )
{
f[i][j] = max(f[i - 1][j], f[i][j - 1]);
if(a[i] == b[j]) f[i][j] = max(f[i][j], f[i - 1][j - 1] + 1);
}
cout << f[n][m] << endl;
return 0;
}
边栏推荐
- 如何优雅的写 Controller 层代码?
- September 23, 2020 left and right values reference std:: move()
- 给定两个整形变量的值,将两个值的内容进行交换 (C语言)
- DevExpress的双击获取单元格数据
- September 21, 2020 referer string segmentation boost gateway code organization level
- mysql 8.0 一条insert语句的具体执行流程分析(二)
- 智能组卷系统设计
- How can I get the stock account opening discount? Also, is it safe to open an account online?
- Reprint: five methods to determine whether an object has attributes
- 2020-9-14 introduction to advertising system
猜你喜欢

AQS之ReentrantLock源码解析

September 29, 2020 non commodity templating code level rapidjson Library

Print prime numbers between 100 and 200 (C language)

Alibaba cloud server is installed and configured with redis. Remote access is unavailable

工具箱之 IKVM.NET 项目新进展

SQL Server 数据库的连接查询

# 【OpenCV 例程200篇】214. 绘制椭圆的参数详解

如何快速完成磁盘分区

Recyclerview sticky (suspended) head

区域工业互联网市场成绩单,百度智能云开物第二
随机推荐
BUUCTF--新年快乐
高效工作必备:测试人如何提高沟通技能?
SQL Server 数据库的统计查询
LVGL库入门教程 - 动画
在实践中学习Spark计算框架(01)
IIS server related error
CLR via C reading notes - single instance application
Download control 1 of custom control (downloadview1)
MySQL中的alter table操作之add/modify/drop列
Contents of advanced mathematics
C#MDI打开子窗体去掉自动生成的菜单栏
攻防世界-Re-insfsay
Recyclerview sticky (suspended) head
Win32exception (0x80004005): This program is blocked by group policy. For more information, contact your system administrator.
BUUCTF--reverse2
BUUCTF--reverse2
C#中Linq常用用法
Virtual machine port scanning
Recurrence of vulnerability analysis for Cisco ASA, FTD and hyperflex HX
Is it safe to open a securities account? Is it reliable?

