hdu 1423 Greatest Common Increasing Subsequence(不连续子序列)

来源:互联网 发布:ps软件有哪些 编辑:程序博客网 时间:2024/05/29 08:33

Greatest Common Increasing Subsequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4841    Accepted Submission(s): 1543


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

最长上升公共子序列,dp,,好好学学啊,唉2015,4,25,,下边是大神的解释  * DP(最长公共递增非连续子序列Greatest Common Increasing Subsequence)  *   设数组f[i][j]代表第一个序列前i个数与第二个序列前j个数的 最长公共递增非连续子序列(GCIS) 的长度。  *   而数组tmp[i][j]则代表第一个序列前i个数与第二个序列前j个数的GCIS的最大数(即最后一个数)  *                        max{f[i-1][j-1],f[i-1][j],f[i][j-1]}    (当a[i]!=a[j])  *                       /  *   状态转移方程:f[i][j]=  *                       \  *                        max{f[i-1][j-1]+1(满足a[i]>tmp[i-1][j-1]),         f[i-1][j-1],f[i-1][j]+1(满足a[i]>tmp[i-1][j]),         f[i-1][j],f[i][j-1]+1(满足a[i]>tmp[i][j-1]),f[i][j-1]}   (当a[i]==a[j])  */  

#include<stdio.h>#include<string.h>#define M 550#define max(a,b) (a>b? a:b)int dp[M][M],a[M],b[M];int main(){int t,m1,m2,i,j,k;scanf("%d",&t);while(t--){scanf("%d",&m1);for(i=1;i<=m1;i++)scanf("%d",&a[i]);scanf("%d",&m2);for(i=1;i<=m2;i++)scanf("%d",&b[i]);memset(dp,0,sizeof(dp));for(i=1;i<=m1;i++){k=0;for(j=1;j<=m2;j++){dp[i][j]=dp[i-1][j];if(b[j]<a[i]&&k<dp[i-1][j])k=dp[i-1][j];if(b[j]==a[i])dp[i][j]=k+1;}}k=-1;for(i=1;i<=m2;i++)if(k<dp[m1][i])k=dp[m1][i];printf("%d\n",k);if(t!=0)printf("\n");}return 0;}


0 0
原创粉丝点击