hdoj problem 1423 Greatest Common Increasing Subsequence (动态规划)

来源:互联网 发布:2345软件大全官方网站 编辑:程序博客网 时间:2024/05/16 07:33

Greatest Common Increasing Subsequence

http://acm.hdu.edu.cn/showproblem.php?pid=1423
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4334    Accepted Submission(s): 1369


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
151 4 2 5 -124-12 1 2 4
 

Sample Output
2
 

Source
ACM暑期集训队练习赛(二)
 

Recommend
lcy   |   We have carefully selected several similar problems for you:  1400 1418 1424 1401 1404 
/*
题目大意是给定两个数字串a、b,求出它们最长公共递增子序列的长度。
状态dp[j]表示a中从0到n-1与b中从1到m-1并以b[j]为结尾的最长公共上升子序列的长度。
状态转移方程:dp[j] = dp[k] + 1,if (a[i] = b[j]&&1 <= k < j)
*/

/*
不明白为什么这种做法就WA求大神解释

WAWAWA
#include<cstdio>
#include<cstring>
#include<stdlib.h>
#include<algorithm>
using namespace std;
int dp[1010];
int a[510];
int b[510];
int Max(int a,int b)
{
return a>b?a:b;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,i,j,k,sum=0,m;
scanf("%d",&n);
for(i=0;i<n;i++)
  scanf("%d",&a[i]);
 
  scanf("%d",&m);
  for(i=0;i<m;i++)
    scanf("%d",&b[i]);
    
    memset(dp,0,sizeof(dp));
    int len=0;
    for(i=1;i<=n;i++)
    {
dp[0]=0;
for(j=1;j<=i;j++)
    {
         if(a[i]>b[j])
           dp[j]=Max(dp[j-1],dp[j]); 
      if(a[i]==b[j])
        dp[j]=Max(dp[j-1],dp[j])+1;
    }
    }
     
 for(i=1;i<=m;i++)
    len=Max(dp[i],len); 
    printf("%d\n",len);  
   if(T)
     printf("\n");
}
return 0;
}
*/
/*AC*/
#include<cstdio>
#include<cstring>
#include<stdlib.h>
#include<algorithm>
using namespace std;
int dp[1010];
int a[510];
int b[510];
int Max(int a,int b)
{
return a>b?a:b;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,i,j,k,sum=0,m;
scanf("%d",&n);
for(i=0;i<n;i++)
  scanf("%d",&a[i]);
 
  scanf("%d",&m);
  for(i=0;i<m;i++)
    scanf("%d",&b[i]);
    
    memset(dp,0,sizeof(dp));
    int len=0,max=0;
    for(i=0;i<n;i++)
    {
    max=0;
for(j=0;j<m;j++)
    {
       if(max<dp[j]&&a[i]>b[j])
          max=dp[j];
       if(a[i]==b[j])
         dp[j]=max+1; 

    }
    }
 for(i=0;i<m;i++)
    len=Max(dp[i],len); 
    printf("%d\n",len);  
    if(T)
    printf("\n");
         
}
return 0;
}

0 0
原创粉丝点击