当前位置:网站首页>[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();
}
边栏推荐
- Principle analysis of security rememberme
- 研究表明“气味相投”更易成为朋友
- Node. JS accessing PostgreSQL database through ODBC
- OpenFOAM:lduMatrix&lduAddressing
- 使用BLoC 构建 Flutter的页面实例
- Winter vacation daily question - lucky numbers in the matrix
- Tupang multi-target tracking! BOT sort: robust correlated multi pedestrian tracking
- 每日一题:1175.质数排列
- JS逆向之行行查data解密
- Daily question: 1175 Prime permutation
猜你喜欢

错误:EACCES:权限被拒绝,访问“/usr/lib/node_modules”

使用BLoC 构建 Flutter的页面实例

Japan bet on national luck: Web3.0, anyway, is not the first time to fail!

Essential for operation and maintenance - Elk log analysis system

SAP MM 因物料有负库存导致MMPV开账期失败问题之对策

Daily practice of C language --- monkeys divide peaches

Find love for speed in F1 delta time Grand Prix
![[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

2、 Frame mode MPLS operation

Engineers who can't read device manuals are not good cooks
随机推荐
SSL证书的分类有哪些?如何选择合适的SSL证书?
[Unity]使用GB2312,打包后程序不正常解决方案
OpenFOAM:lduMatrix&lduAddressing
OpenAPI generator: simplify the restful API development process
能自动更新的万能周报模板,有手就会用!
科技的成就(二十七)
不会看器件手册的工程师不是个好厨子
Redis database persistence
Three methods of finding LCA of the nearest common ancestor
[youcans' image processing learning course] general contents
Winter vacation daily question - lucky numbers in the matrix
Detailed collection of common MySQL commands
Pattern matching and regular expressions in PostgreSQL - Das
题解:《你的飞碟在这儿》、《哥德巴赫猜想》
[技术发展-22]:网络与通信技术的应用与发展快速概览-2- 通信技术
mac(macos Monterey12.2 m1) 个人使用php开发
mysql ---- Oracle中的rownum转换成MySQL
每日一题:1175.质数排列
互联网常见34个术语解释
三翼鸟两周年:羽翼渐丰,腾飞指日可待