码迷,mamicode.com
首页 > 其他好文 > 详细

dp(最长公共上升子序列)

时间:2019-07-24 15:09:56      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:you   desc   greatest   set   子序列   scribe   script   style   http   

This is a problem from ZOJ 2432.To make it easyer,you just need output the length of the subsequence.

InputEach sequence is described with M - its length (1 <= M <= 500) and M integer numbers Ai (-2^31 <= Ai < 2^31) - the sequence itself.Outputoutput print L - the length of the greatest common increasing subsequence of both sequences.Sample Input
1

5
1 4 2 5 -12
4
-12 1 2 4
Sample Output
2
lis(最长上升子序列)和lcs(最长公共子序列)的结合lcis(最长公共上升子序列)还不是很懂这个问题
https://www.cnblogs.com/WArobot/p/7479431.html
#include <iostream>
#include <iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include <stdio.h>
#include <string.h>
using namespace std;

int w[109] , dp[1009] , a[1009] , b[1009];

int main()
{
    int n ;
    scanf("%d" , &n);
    while(n--)
    {
        int m  , l;
        scanf("%d" , &m);
        memset(dp , 0 , sizeof(dp));
        for(int i = 1 ; i <= m ; i++)
        {
            scanf("%d" , &a[i]);
        }
        scanf("%d" , &l);
        for(int i = 1 ; i <= l ; i++)
        {
            scanf("%d" , &b[i]) ;
        }
        int mas = 0 ;
        for(int i = 1 ; i <= m ; i++)
        {
            mas = 0 ; // 记录b数组前j个与a数组前i个的最长公共升序列的个数
            for(int j = 1 ; j <=l ; j++)
            {
                if(a[i] > b[j])
                    mas = max(mas , dp[j]);
                if(a[i] == b[j])
                    dp[j] = mas + 1 ;
            }
        }
        int ans = 0 ;
        for(int i = 1 ; i <= l ; i++)
        {
            ans = max(ans , dp[i]);
        }
        if(n)
        {
            printf("%d\n\n", ans);
        }
        else
            printf("%d\n" , ans);


    }


    return 0;
}

 

 

dp(最长公共上升子序列)

标签:you   desc   greatest   set   子序列   scribe   script   style   http   

原文地址:https://www.cnblogs.com/nonames/p/11237948.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!