当前位置:网站首页>[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;
}
边栏推荐
- label问题排查:打不开标注好的图像
- Machine learning: the concept and application of VC dimension
- Analysis of common vlog parameters
- MySQL multi table query
- 云原生爱好者周刊:炫酷的 Grafana 监控面板集合
- Unity about failure (delay) of destroy and ondestroy
- 8 software engineering environment
- Solr基础操作12
- How to realize the spelling correction function in search engine
- Clone undirected graph [bfs accesses each edge but not only nodes]
猜你喜欢

Siemens low code platform connects MySQL through database connector to realize addition, deletion, modification and query
![[advanced C language] string and memory function (I)](/img/fa/5531253940d99f2646cb6964992e7c.png)
[advanced C language] string and memory function (I)

Unity splashimage scaling problem

剑指 Offer II 037. 小行星碰撞
500 error occurred after importing skins folder into solo blog skin

爬虫入门实战:斗鱼弹幕数据抓取,附送11节入门笔记

6.29 problem solving
![克隆無向圖[bfs訪問每條邊而不止節點]](/img/34/2a1b737b6095293f868ec6aec0ceeb.png)
克隆無向圖[bfs訪問每條邊而不止節點]

Teach you step by step to install webwind notes on edge browser

QT learning 02 GUI program example analysis
随机推荐
Official website of Greentree
Virtual machine online migration based on openstack
[advanced C language] dynamic memory management
Solr basic operation 16
Machine learning: the concept and application of VC dimension
Solr基础操作6
手机开户后多久才能通过?另外,手机开户安全么?
Leetcode (76) -- Minimum Covering substring
代码分析平台 SonarQube 实战
多数元素II[求众数类之摩尔投票法]
Three postures of anti CSRF blasting
Golang6 reflection
Project 1: deploy lamp ECSHOP e-commerce platform
Majority element ii[molar voting method for finding modes]
Solr basic operation 11
There is no web-based development for the reward platform. Which is suitable for native development or mixed development?
【毕业季|进击的技术er】工作七年的职场人,不希望你们再走弯路
Root cause of glideexception: failed decodepath{directbytebuffer- > gifdrawable- > drawable}
Solr基础操作11
After crossing, she said that the multiverse really exists

