当前位置:网站首页>[template] longest common subsequence ([DP or greedy] board)
[template] longest common subsequence ([DP or greedy] board)
2022-07-02 13:45:00 【Joanh_ Lan】
【 Templates 】 Longest common subsequence
Title Description
give 1 , 2 , … , n 1,2,\ldots,n 1,2,…,n Two permutations of P 1 P_1 P1 and P 2 P_2 P2 , Find their longest common subsequence .
Input format
The first line is a number n n n.
The next two lines , Every act n n n Number , For natural Numbers 1 , 2 , … , n 1,2,\ldots,n 1,2,…,n An arrangement of .
Output format
a number , That is, the length of the longest common subsequence .
Examples #1
The sample input #1
5
3 2 1 4 5
1 2 3 4 5
Sample output #1
3
Tips
- about 50 % 50\% 50% The data of , n ≤ 1 0 3 n \le 10^3 n≤103;
- about 100 % 100\% 100% The data of , n ≤ 1 0 5 n \le 10^5 n≤105.
DP ( o ( n 2 ) o(n^2) o(n2))
Templates
#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();
}
greedy ( o ( n l o g n ) o(nlogn) o(nlogn))
Templates
#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();
}
边栏推荐
- A better database client management tool than Navicat
- Error function ERF
- Gee learning notes 2
- Embedded software development
- JS reverse massive creative signature
- When tidb meets Flink: tidb efficiently enters the lake "new play" | tilaker team interview
- mysql ---- Oracle中的rownum转换成MySQL
- 大家信夫一站式信用平台让信用场景“用起来
- P3807 [template] Lucas theorem /lucas theorem
- Security RememberMe原理分析
猜你喜欢
【蓝桥杯选拔赛真题43】Scratch航天飞行 少儿编程scratch蓝桥杯选拔赛真题讲解
[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
题解《子数整数》、《欢乐地跳》、《开灯》
Bridge of undirected graph
Three methods of finding LCA of the nearest common ancestor
Unity small map production [2]
BeanUtils--浅拷贝--实例/原理
(POJ - 1984) navigation nightare (weighted and search set)
能自动更新的万能周报模板,有手就会用!
Can automatically update the universal weekly report template, you can use it with your hand!
随机推荐
免费SSL证书知多少?免费SSL证书和收费SSL证书的区别
Skillfully use SSH to get through the Internet restrictions
How much do you know about free SSL certificates? The difference between free SSL certificate and charged SSL certificate
SSL证书的分类有哪些?如何选择合适的SSL证书?
为什么switch 的default后面要跟break?
Solution: Compression Technology (original version and sequel version)
Android kotlin fragment technology point
Qt-制作一个简单的计算器-实现四则运算
What are eNB, EPC and PGW?
What are the classifications of SSL certificates? How to choose the appropriate SSL certificate?
验证失败,请检查您的回电网址。您可以按照指导进行操作
Fundamentals of machine learning (II) -- division of training set and test set
【模板】最长公共子序列 (【DP or 贪心】板子)
Independent and controllable 3D cloud CAD: crowncad enables innovative design of enterprises
Node. JS accessing PostgreSQL database through ODBC
代码实现MNLM
(POJ - 1308)Is It A Tree? (tree)
[Blue Bridge Cup] children's worship circle
不会看器件手册的工程师不是个好厨子
[unity] using GB2312, the solution to abnormal program after packaging