50tps求方法

P1439 【模板】最长公共子序列

control_our_own_life @ 2023-07-21 16:53:11

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 1e3 + 5;
int a[N], b[N], n, dp[N][N];

int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf("%d", &a[i]);
    for (int i = 1; i <= n; i++)
        scanf("%d", &b[i]);
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (a[i] != b[j])
                dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
            else
                dp[i][j] = dp[i - 1][j - 1] + 1;
        }
    }
    printf("%d", dp[n][n]);
    return 0;
}/*
5
1 2 3 2 1
5
3 2 1 4 7
answer:
3
*/

我的方法是n^2,过不了,求调……


by BlackPanda @ 2023-07-21 17:03:52

@niuzeyu1 加二分


by qinshi0308 @ 2023-07-21 17:05:45

题解了解一下谢谢喵


by justalearner @ 2023-07-21 17:07:07

数据范围是n≤10^5哦


by control_our_own_life @ 2023-07-21 17:11:35

@Queue_Hao oh!


by cai_J @ 2023-08-11 17:23:19

省流:暴力只配得50分


|