首页 > 其他分享 >HZOJ 最长公共上升子序列 动态规划

HZOJ 最长公共上升子序列 动态规划

时间:2022-12-29 13:22:05浏览次数:31  
标签:int MAX HZOJ ans 序列 include 最长 dp

题面:

 

解题思路:

首先定义状态dp[i][j]表示序列ai和序列bj的最长公共上升子序列的长度

 

 

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
#include <cmath>
#include <queue>
using namespace std;
#define MAX_N 3000
int a[MAX_N + 5], b[MAX_N + 5];
int ans = 0;

int dp[MAX_N + 5][MAX_N + 5];

int main() {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i <= n; i++) cin >> b[i];
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            dp[i][j] = dp[i][j - 1];
            if (a[i] - b[j]) continue;
            dp[i][j] = max(1, dp[i][j]);
            for (int k = 1; k < i; k++) {
                if (a[k] >= a[i]) continue;
                dp[i][j] = max(dp[i][j], dp[k][j - 1] + 1);
            }
            ans = max(ans, dp[i][j]);
        }
    }
    cout << ans << endl;
    return 0;
}

 

标签:int,MAX,HZOJ,ans,序列,include,最长,dp
From: https://www.cnblogs.com/anaxiansen/p/17012295.html

相关文章