当前位置:网站首页>【模板】最长公共子序列 (【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();
}
边栏推荐
- 【蓝桥杯选拔赛真题43】Scratch航天飞行 少儿编程scratch蓝桥杯选拔赛真题讲解
- Quantum three body problem: Landau fall
- 三谈exception——错误处理
- 题解《子数整数》、《欢乐地跳》、《开灯》
- 操作教程:EasyDSS如何将MP4点播文件转化成RTSP视频流?
- Independent and controllable 3D cloud CAD: crowncad enables innovative design of enterprises
- [unity] using GB2312, the solution to abnormal program after packaging
- [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
- ADB basic commands
- We sincerely invite young creators to share with investors and entrepreneurs how to make choices in life in the metauniverse
猜你喜欢
【笔耕不辍勋章活动】生命不止,写作不息
挥发性有机物TVOC、VOC、VOCS气体检测+解决方案
Unity skframework framework (XVII), freecameracontroller God view / free view camera control script
Lucky numbers in the [leetcode daily question] matrix
为什么switch 的default后面要跟break?
题解:《压缩技术》(原版、续集版)
三翼鸟两周年:羽翼渐丰,腾飞指日可待
net share
de4000h存储安装配置
Unity skframework framework (XIII), question module
随机推荐
Drawing Nyquist diagram with MATLAB
TVOC, VOC, VOCs gas detection + Solution
(POJ - 1308)Is It A Tree? (tree)
Engineers who can't read device manuals are not good cooks
Essential for operation and maintenance - Elk log analysis system
Achievements in science and Technology (27)
Unity skframework framework (XVII), freecameracontroller God view / free view camera control script
De4000h storage installation configuration
[Unity]使用GB2312,打包后程序不正常解决方案
Unity SKFramework框架(二十一)、Texture Filter 贴图资源筛选工具
Let juicefs help you with "remote backup"
Android kotlin fragment technology point
Which do you choose between Alibaba P7 with an annual salary of 900000 and deputy department level cadres?
MySQL: Invalid GIS data provided to function st_ geometryfromtext
Integral link, inertia link and proportion link in Simulink
Unity skframework framework (XX), VFX lab special effects library
三谈exception——错误处理
Verification failed, please check your call back website. You can follow the instructions
I did it with two lines of code. As a result, my sister had a more ingenious way
Can automatically update the universal weekly report template, you can use it with your hand!