当前位置:网站首页>【模板】最长公共子序列 (【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();
}
边栏推荐
- Node. JS accessing PostgreSQL database through ODBC
- Principle analysis of security rememberme
- Memory management 01 - link script
- Can automatically update the universal weekly report template, you can use it with your hand!
- Unity skframework framework (XVIII), roamcameracontroller roaming perspective camera control script
- leetcode621. task scheduler
- 机器学习基础(二)——训练集和测试集的划分
- Unity skframework framework (XX), VFX lab special effects library
- Operation tutorial: how does easydss convert MP4 on demand files into RTSP video streams?
- TVOC, VOC, VOCs gas detection + Solution
猜你喜欢

Solve "sub number integer", "jump happily", "turn on the light"

Node.js通过ODBC访问PostgreSQL数据库
![[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](/img/42/21f6d0fdd159faa8b63713624c95a2.png)
[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

【云原生数据库】遇到慢SQL该怎么办(上)?

Performance optimization of memory function
![[technology development-22]: rapid overview of the application and development of network and communication technology-2-communication Technology](/img/a7/44609a5acf25021f1fca566c3d8c90.png)
[technology development-22]: rapid overview of the application and development of network and communication technology-2-communication Technology

Unity SKFramework框架(十二)、Score 计分模块

Redis database persistence

Unity skframework Framework (XVI), package manager Development Kit Manager

Unity skframework framework (XXI), texture filter map resource filtering tool
随机推荐
伙伴云表格强势升级!Pro版,更非凡!
JS reverse row query data decryption
Solve "sub number integer", "jump happily", "turn on the light"
Unity skframework framework (XV), singleton singleton
nohup命令
Pointer from entry to advanced (1)
MAC (MacOS Monterey 12.2 M1) personal use PHP development
[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
leetcode621. 任务调度器
Don't spend money, spend an hour to build your own blog website
Unity SKFramework框架(十三)、Question 问题模块
leetcode621. task scheduler
Uniapp develops wechat applet Tencent map function and generates sig signature of location cloud
We sincerely invite young creators to share with investors and entrepreneurs how to make choices in life in the metauniverse
基于ssm+jsp框架实现的学生选课信息管理系统【源码+数据库】
Three talking about exception -- error handling
Error function ERF
Memory management 01 - link script
Unity skframework framework (XVII), freecameracontroller God view / free view camera control script
What are the classifications of SSL certificates? How to choose the appropriate SSL certificate?