当前位置:网站首页>【模板】最长公共子序列 (【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();
}
边栏推荐
- Redis database persistence
- 免费SSL证书知多少?免费SSL证书和收费SSL证书的区别
- OpenAPI generator: simplify the restful API development process
- 文件的下载与图片的预览
- Pocket Raider comments
- 屠榜多目标跟踪!BoT-SORT:稳健的关联多行人跟踪
- 诚邀青年创作者,一起在元宇宙里与投资人、创业者交流人生如何做选择……...
- How much do you know about free SSL certificates? The difference between free SSL certificate and charged SSL certificate
- Explanation of 34 common terms on the Internet
- Web基础
猜你喜欢

Web基础
![Unity small map production [2]](/img/d6/9d6556d37525b9986b74133f2a7aaa.jpg)
Unity small map production [2]

为什么switch 的default后面要跟break?

SAP MM 因物料有负库存导致MMPV开账期失败问题之对策

What are eNB, EPC and PGW?

OpenFOAM:lduMatrix&lduAddressing

Unity SKFramework框架(二十)、VFX Lab 特效库

mac(macos Monterey12.2 m1) 个人使用php开发

诚邀青年创作者,一起在元宇宙里与投资人、创业者交流人生如何做选择……...

2022 Heilongjiang provincial examination on the writing skills of Application Essays
随机推荐
Chinese name extraction (toy code - accurate head is too small, right to play)
最近公共祖先LCA的三种求法
P3807 [template] Lucas theorem /lucas theorem
Daily question: 1175 Prime permutation
Unity skframework framework (XVIII), roamcameracontroller roaming perspective camera control script
D为何链接不了dll
Unity SKFramework框架(十五)、Singleton 单例
Record idea shortcut keys
Word efficiency guide - word's own template
Countermeasures for the failure of MMPV billing period caused by negative inventory of materials in SAP mm
JS generates 4-digit verification code
de4000h存储安装配置
D how to check null
Tupang multi-target tracking! BOT sort: robust correlated multi pedestrian tracking
诚邀青年创作者,一起在元宇宙里与投资人、创业者交流人生如何做选择……...
题解《子数整数》、《欢乐地跳》、《开灯》
挥发性有机物TVOC、VOC、VOCS气体检测+解决方案
Android kotlin material design technology points
免费SSL证书知多少?免费SSL证书和收费SSL证书的区别
[技术发展-22]:网络与通信技术的应用与发展快速概览-2- 通信技术