浏览器标题切换
浏览器标题切换end

HDU1423-Greatest Common Increasing Subsequence

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

Input
    Each sequence is described with M - its length (1 <= M <= 500) and M integer numbers Ai (-2^31 <= Ai < 2^31) - the sequence itself.
 

Output
    output 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

 

 

题意:

求出给定的两个数组的最长公共上升子序列,输出最长长度即可。

dp记录的是最长长度,记得清空

 

 1 #include<stdio.h>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cmath>
 5 #include<iomanip>
 6 #include<string.h>
 7 using namespace std;
 8 
 9 int a[520],b[520],dp[520];
10 
11 int main()
12 {
13     int tt,p,q;
14     scanf("%d",&tt);
15     while(tt--)
16     {
17         memset(dp,0,sizeof(dp));
18         scanf("%d",&p);
19         for(int i=0;i<p;i++)
20             scanf("%d",&a[i]);
21         scanf("%d",&q);
22         for(int i=0;i<q;i++)
23             scanf("%d",&b[i]);
24         int maxx;
25         for(int i=0;i<p;i++)
26         {
27             maxx=0;//注意一下,这里不能设置为负无穷,因为dp记录最长长度,最小是为0
28             for(int j=0;j<q;j++)
29             {
30                 if(a[i]>b[j])
31                     maxx=max(dp[j],maxx);
32                 else if(a[i]==b[j])
33                     dp[j]=maxx+1;
34             }
35         }
36         int ans=0;
37         for(int i=0;i<q;i++)
38             ans=max(ans,dp[i]);
39         if(tt)
40             printf("%d\n\n",ans);
41         else
42             printf("%d\n",ans);
43     }
44     return 0;
45 }

 

posted @ 2019-09-12 16:49  抓水母的派大星  阅读(169)  评论(0)    收藏  举报