本文主要是介绍J - Greatest Common Increasing Subsequence,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目:
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
15 1 4 2 5 -12 4 -12 1 2 4
Sample Output
2
题意:
这道题要求两个数列的最长的上升公共子序列;
代码如下:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;int t,n,m;
int a[1001],b[1001];int dp[1001];int main()
{scanf("%d",&t);while(t--){int i,j;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);for(i=0;i<n;i++){int max=0;for(j=0;j<m;j++){if(a[i]>b[j]&&max<dp[j])max=dp[j];if(a[i]==b[j])dp[j]=max+1;}}int max=0;for(i=0;i<m;i++){if(dp[i]>max)max=dp[i];}printf("%d\n",max);if(t!=0)printf("\n");}return 0;
}
这篇关于J - Greatest Common Increasing Subsequence的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!