当前位置:网站首页>【动态规划】—— 线性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;
}
边栏推荐
- SQL Server 数据库增删改查语句
- mysql 8.0 一条insert语句的具体执行流程分析(二)
- MySQL中的alter table操作之add/modify/drop列
- 由ASP.NET Core根据路径下载文件异常引发的探究
- Recyclerview sticky (suspended) head
- 全面理解Synchronized
- September 17, 2020 gateway business process has two tasks: referer certification and non commodity Templating
- Web vulnerability manual detection and analysis
- Implementation of iequalitycomparer interface in C #
- 《如何阅读一本书》读后总结
猜你喜欢

真正的测试 =“半个产品+半个开发”?

《CLR via C#》读书笔记-单实例应用程序

Automatic 3D Detection and Segmentation of Head and Neck Cancer from MRI Data.

Recurrence of vulnerability analysis for Cisco ASA, FTD and hyperflex HX

Print prime numbers between 100 and 200 (C language)

2020-10-17:刷题1

C#中Linq常用用法

查看CSDN的博客排名

How to quickly complete disk partitioning

Solve the problem that zxing's QR code contains Chinese garbled code
随机推荐
I would like to know how to open an account for free online stock registration? In addition, is it safe to open a mobile account?
《CLR via C#》读书笔记-加载与AppDomain
产品力不输比亚迪,吉利帝豪L雷神Hi·X首月交付1万台
《CLR via C#》读书笔记-单实例应用程序
October 17, 2020: question brushing 1
2020-9-14 introduction to advertising system
Given the values of two integer variables, the contents of the two values are exchanged (C language)
Win32Exception (0x80004005): 组策略阻止了这个程序。要获取详细信息,请与系统管理员联系。
Buuctf-- connotative software
PGP在加密技术中的应用
mysql 8.0 一条insert语句的具体执行流程分析(二)
Automatic 3D Detection and Segmentation of Head and Neck Cancer from MRI Data.
Arc view and arc viewpager
Win32exception (0x80004005): This program is blocked by group policy. For more information, contact your system administrator.
Atomic explanation of AQS
BUUCTF RE-easyre
你的项目需要自动化测试吗?
【评论送书】适合初学者的 6 个有趣的 R 语言项目
F5 big IP Icontrol rest command execution (cve-2022-1388)
C # use winexec to call exe program

