LCIS 最长公共上升子序列

题解在这儿:https://www.luogu.com.cn/blog/NashChen/LCIS-Problem

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN=1e3+10;
int n,m;
int f[MAXN][MAXN],a[MAXN],b[MAXN];
int main() {
    cin>>n>>m;
    for (int i=1;i<=n;i++) cin>>a[i];
    for (int j=1;j<=m;j++) cin>>b[j];
    /*
        dp[i][j]  if a[i]==b[j]   dp[i-1][k]  1<=k<j  b[k]<a[i]
        else 
    */
    for (int i=1;i<=n;i++) {
        int now=0;
        for (int j=1;j<=m;j++) {
            if (a[i]==b[j]) {
                f[i][j]=now+1;
            }
            else {
                f[i][j]=f[i-1][j];
            }
            if (b[j]<a[i]&&f[i-1][j]>now) {
                now=f[i-1][j];
            }
            // cout<<f[i][j]<<" "<<now<<" ";
        }
        // cout<<endl;
    }
    int ans=0;
    for (int j=1;j<=m;j++) ans=max(ans,f[n][j]);
    cout<<ans<<endl;
    return 0;
}
/*
4 4
4 2 3 1
2 4 3 1
*/

猜你喜欢

转载自www.cnblogs.com/xyqxyq/p/12632833.html