当前位置:网站首页>【模板】最长公共子序列 (【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();
}
边栏推荐
- Can automatically update the universal weekly report template, you can use it with your hand!
- leetcode621. 任务调度器
- Nohup command
- Skillfully use SSH to get through the Internet restrictions
- What are eNB, EPC and PGW?
- Security RememberMe原理分析
- SAP MM 因物料有负库存导致MMPV开账期失败问题之对策
- 三谈exception——错误处理
- OpenApi-Generator:简化RESTful API开发流程
- Redis database persistence
猜你喜欢

Pointer from entry to advanced (1)

屠榜多目标跟踪!BoT-SORT:稳健的关联多行人跟踪

研究表明“气味相投”更易成为朋友

Node.js通过ODBC访问PostgreSQL数据库

TVOC, VOC, VOCs gas detection + Solution

Countermeasures for the failure of MMPV billing period caused by negative inventory of materials in SAP mm

The second anniversary of the three winged bird: the wings are getting richer and the take-off is just around the corner

Unity skframework framework (XVIII), roamcameracontroller roaming perspective camera control script

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

题解:《你的飞碟在这儿》、《哥德巴赫猜想》
随机推荐
Node. JS accessing PostgreSQL database through ODBC
量子三体问题: Landau Fall
Web基础
Don't spend money, spend an hour to build your own blog website
Unity SKFramework框架(十三)、Question 问题模块
Find love for speed in F1 delta time Grand Prix
On flow delivery between microservices
[cloud native database] what to do when encountering slow SQL (Part 1)?
不会看器件手册的工程师不是个好厨子
Unity skframework Framework (XVI), package manager Development Kit Manager
Can automatically update the universal weekly report template, you can use it with your hand!
Integral link, inertia link and proportion link in Simulink
Unity SKFramework框架(十四)、Extension 扩展函数
JS逆向之巨量创意signature签名
Professor of Shanghai Jiaotong University: he Yuanjun - bounding box (containment / bounding box)
SSL证书的分类有哪些?如何选择合适的SSL证书?
ADB basic commands
Download files and preview pictures
【笔耕不辍勋章活动】生命不止,写作不息
文件的下载与图片的预览