当前位置:网站首页>[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();
}
边栏推荐
- What are eNB, EPC and PGW?
- Principle analysis of security rememberme
- Solution: Compression Technology (original version and sequel version)
- [technology development-22]: rapid overview of the application and development of network and communication technology-2-communication Technology
- The xftp connection Haikang camera reported an error: the SFTP subsystem application has been rejected. Please ensure that the SFTP subsystem settings of the SSH connection are valid
- 【文档树、设置】字体变小
- Web基础
- De4000h storage installation configuration
- Android kotlin material design technology points
- Crowncad (crown CAD), the first fully independent 3D CAD platform based on Cloud Architecture in China
猜你喜欢

Node.js通过ODBC访问PostgreSQL数据库
![[Blue Bridge Cup] children's worship circle](/img/ad/5af4fe76ad5d1426bc460904d7f049.jpg)
[Blue Bridge Cup] children's worship circle

最近公共祖先LCA的三种求法

基于ssm+jsp框架实现的学生选课信息管理系统【源码+数据库】

How to modify the error of easydss on demand service sharing time?

(POJ - 1984) navigation nightare (weighted and search set)

Solve "sub number integer", "jump happily", "turn on the light"
![[indomitable medal activity] life goes on and writing goes on](/img/c1/54e3f1b37db25af3f1998b39da301b.png)
[indomitable medal activity] life goes on and writing goes on
![[cloud native database] what to do when encountering slow SQL (Part 1)?](/img/ac/a59189aba1901e769beec1ec7e6d32.png)
[cloud native database] what to do when encountering slow SQL (Part 1)?

Three methods of finding LCA of the nearest common ancestor
随机推荐
如何设置Qt手工布局
Essential for operation and maintenance - Elk log analysis system
[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
D language, possible 'string plug-ins'
基于ssm+jsp框架实现的学生选课信息管理系统【源码+数据库】
不会看器件手册的工程师不是个好厨子
使用BLoC 构建 Flutter的页面实例
(POJ - 1984) navigation nightare (weighted and search set)
JS逆向之巨量创意signature签名
Numpy array calculation
How much do you know about free SSL certificates? The difference between free SSL certificate and charged SSL certificate
Nohup command
Origin绘制热重TG和微分热重DTG曲线
MAC (MacOS Monterey 12.2 M1) personal use PHP development
Independent and controllable 3D cloud CAD: crowncad enables innovative design of enterprises
[Blue Bridge Cup] children's worship circle
代码实现MNLM
OpenAPI generator: simplify the restful API development process
石子合并板子【区间DP】(普通石子合并 & 环形石子合并)
[technology development-22]: rapid overview of the application and development of network and communication technology-2-communication Technology