当前位置:网站首页>【模板】最长公共子序列 (【DP or 贪心】板子)
【模板】最长公共子序列 (【DP or 贪心】板子)
2022-07-02 10:14:00 【Joanh_Lan】
【模板】最长公共子序列
题目描述
给出 1 , 2 , … , n 1,2,\ldots,n 1,2,…,n 的两个排列 P 1 P_1 P1 和 P 2 P_2 P2 ,求它们的最长公共子序列。
输入格式
第一行是一个数 n n n。
接下来两行,每行为 n n n 个数,为自然数 1 , 2 , … , n 1,2,\ldots,n 1,2,…,n 的一个排列。
输出格式
一个数,即最长公共子序列的长度。
样例 #1
样例输入 #1
5
3 2 1 4 5
1 2 3 4 5
样例输出 #1
3
提示
- 对于 50 % 50\% 50% 的数据, n ≤ 1 0 3 n \le 10^3 n≤103;
- 对于 100 % 100\% 100% 的数据, n ≤ 1 0 5 n \le 10^5 n≤105。
DP ( o ( n 2 ) o(n^2) o(n2))
模板
#include <bits/stdc++.h>
#define buff \
ios::sync_with_stdio(false); \
cin.tie(0);
//#define int long long
using namespace std;
const int N = 1e3 + 9;
int n;
int s1[N], s2[N];
int dp[N][N];
void solve()
{
cin >> n;
for (int i = 1; i <= n; i++)
cin >> s1[i];
for (int i = 1; i <= n; i++)
cin >> s2[i];
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
if (s1[i] == s2[j])
dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1);
}
}
cout << dp[n][n] << '\n';
}
int main()
{
solve();
}
贪心 ( o ( n l o g n ) o(nlogn) o(nlogn))
模板
#include <bits/stdc++.h>
#define buff \
ios::sync_with_stdio(false); \
cin.tie(0);
//#define int long long
using namespace std;
const int N = 1e5 + 9;
int n;
int s[N];
int q[N];
void solve()
{
cin >> n;
for (int i = 1; i <= n; i++)
{
int t;
cin >> t;
s[t] = i;
}
memset(q, 0x3f, sizeof q);
for (int i = 1; i <= n; i++)
{
int t;
cin >> t;
int x = s[t];
*lower_bound(q + 1, q + 1 + n, x) = x;
}
cout << lower_bound(q + 1, q + 1 + n, q[0]) - q - 1 << '\n';
}
int main()
{
solve();
}
边栏推荐
- Unity skframework framework (XII), score scoring module
- 题解:《你的飞碟在这儿》、《哥德巴赫猜想》
- [technology development-22]: rapid overview of the application and development of network and communication technology-2-communication Technology
- 中文姓名提取(玩具代码——准头太小,权当玩闹)
- EasyDSS点播服务分享时间出错如何修改?
- Memory management 01 - link script
- 【youcans 的图像处理学习课】总目录
- 能自动更新的万能周报模板,有手就会用!
- 挥发性有机物TVOC、VOC、VOCS气体检测+解决方案
- 屠榜多目标跟踪!BoT-SORT:稳健的关联多行人跟踪
猜你喜欢
Unity skframework Framework (XVI), package manager Development Kit Manager
OpenAPI generator: simplify the restful API development process
[true topic of the Blue Bridge Cup trials 43] scratch space flight children's programming explanation of the true topic of the Blue Bridge Cup trials
【OpenGL】笔记二十九、高级光照(镜面高光)
Unity SKFramework框架(二十一)、Texture Filter 贴图资源筛选工具
We sincerely invite young creators to share with investors and entrepreneurs how to make choices in life in the metauniverse
EasyDSS点播服务分享时间出错如何修改?
Unity skframework framework (XVI), package manager development kit Manager
Tupang multi-target tracking! BOT sort: robust correlated multi pedestrian tracking
屠榜多目标跟踪!BoT-SORT:稳健的关联多行人跟踪
随机推荐
科技的成就(二十七)
Unity SKFramework框架(二十)、VFX Lab 特效库
[cloud native database] what to do when encountering slow SQL (Part 1)?
Security RememberMe原理分析
D如何检查null
The 29 year old programmer in Shanghai was sentenced to 10 months for "deleting the database and running away" on the day of his resignation!
[true topic of the Blue Bridge Cup trials 43] scratch space flight children's programming explanation of the true topic of the Blue Bridge Cup trials
Unity skframework framework (XVII), freecameracontroller God view / free view camera control script
Which do you choose between Alibaba P7 with an annual salary of 900000 and deputy department level cadres?
D为何链接不了dll
OpenApi-Generator:简化RESTful API开发流程
MySQL: Invalid GIS data provided to function st_ geometryfromtext
leetcode621. 任务调度器
mac(macos Monterey12.2 m1) 个人使用php开发
Three talking about exception -- error handling
[Blue Bridge Cup] children's worship circle
[youcans' image processing learning course] general contents
de4000h存储安装配置
Bridge of undirected graph
How to explain binary search to my sister? This is really difficult, fan!