当前位置:网站首页>[dynamic programming] - linear DP
[dynamic programming] - linear DP
2022-06-30 00:12:00 【Xuanche_】

Example :AcWing 898. Digital triangle


AC Code
#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;
}Example :AcWing 895. Longest ascending subsequence (LIS)



Unadorned DP The formula :
#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; // Only a[i] The case of a number
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;
}Record the longest ascending subsequence ( The reverse )
#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; // Only a[i] The case of a number
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. Longest common subsequence


#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;
}
边栏推荐
- Summarize Flink runtime architecture in simple terms
- 【毕业季|进击的技术er】工作七年的职场人,不希望你们再走弯路
- JS的初步语法
- How to realize the spelling correction function in search engine
- Viewing splitchunks code segmentation from MPX resource construction optimization
- 一步步教你在Edge浏览器上安装网风笔记
- This PMP Exam (June 25), some people are happy and others are worried. That's why
- Embedded development: Hardware in the loop testing
- Solr basic operation 8
- 利用 CertBot 申请 Let’s Encrypt SSL 证书
猜你喜欢

Stack space of JVM

由GlideException: Failed DecodePath{DirectByteBuffer->GifDrawable->Drawable}引起的刨根问底

Binary search tree 230 The element with the smallest K in the binary search tree 1038 From binary search tree to larger sum tree

AI首席架构师9-胡晓光 《飞桨模型库与行业应用》

FPGA Development (1) -- serial port communication

Summarize Flink runtime architecture in simple terms
![[rust weekly library] Tokei - a utility for statistics of code lines and other information](/img/6c/4569cc0edaa01e4605c9c256193c31.png)
[rust weekly library] Tokei - a utility for statistics of code lines and other information

How about counting Berry Pie 4? What are the possible ways to play?
![[advanced C language] string and memory function (II)](/img/1a/14ff6a078419e407845d60485be60e.png)
[advanced C language] string and memory function (II)

Construction of module 5 of actual combat Battalion
随机推荐
Solr基础操作6
AI首席架构师9-胡晓光 《飞桨模型库与行业应用》
Analysis of common vlog parameters
Serialization of binary tree 297 Serialization and deserialization of binary tree 652 Find duplicate subtrees
How about counting Berry Pie 4? What are the possible ways to play?
modelsim的TCL脚本的define incdir命令解析
Embedded development: Hardware in the loop testing
Machine learning: the concept and application of VC dimension
Summary of DOM knowledge points
Getting started with qpainter: drawing the chess interface
Unity splashimage scaling problem
What is flush software? Is it safe to open an account online?
【毕业季|进击的技术er】工作七年的职场人,不希望你们再走弯路
爬虫入门实战:斗鱼弹幕数据抓取,附送11节入门笔记
Solr基础操作12
JVM之栈空间
一步步教你在Edge浏览器上安装网风笔记
DOM 知识点总结
Siemens low code platform connects MySQL through database connector to realize addition, deletion, modification and query
Solr basic operations 12

