当前位置:网站首页>[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();
}
边栏推荐
- D language, possible 'string plug-ins'
- Unity small map production [2]
- 免费SSL证书知多少?免费SSL证书和收费SSL证书的区别
- 【OpenGL】笔记二十九、高级光照(镜面高光)
- Find love for speed in F1 delta time Grand Prix
- Sum of the first n terms of Fibonacci (fast power of matrix)
- Drawing Nyquist diagram with MATLAB
- Why is the default of switch followed by break?
- What are eNB, EPC and PGW?
- Android kotlin fragment technology point
猜你喜欢

Independent and controllable 3D cloud CAD: crowncad enables innovative design of enterprises

Essential for operation and maintenance - Elk log analysis system

Common options of tcpdump command: Three

记忆函数的性能优化

Can automatically update the universal weekly report template, you can use it with your hand!

操作教程:EasyDSS如何将MP4点播文件转化成RTSP视频流?

Why is the default of switch followed by break?

How to explain binary search to my sister? This is really difficult, fan!

Three methods of finding LCA of the nearest common ancestor

How to modify the error of easydss on demand service sharing time?
随机推荐
Numpy array calculation
How to use SAP's metadata framework (MDF) to build custom business rules?
Tupang multi-target tracking! BOT sort: robust correlated multi pedestrian tracking
Explanation of 34 common terms on the Internet
Research shows that "congenial" is more likely to become friends
验证失败,请检查您的回电网址。您可以按照指导进行操作
How to explain binary search to my sister? This is really difficult, fan!
Redis数据库持久化
Dingtalk 发送消息
Countermeasures for the failure of MMPV billing period caused by negative inventory of materials in SAP mm
Web基础
三谈exception——错误处理
运维必备——ELK日志分析系统
Web Foundation
Unity skframework framework (XII), score scoring module
Drawing Nyquist diagram with MATLAB
Android kotlin material design technology points
记忆函数的性能优化
[USACO05JAN]Watchcow S(欧拉回路)
(POJ - 1984) navigation nightare (weighted and search set)