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
这道题,给出两个序列a和b,求最长公共递增子序列。解法还是用dp去写,动态转移方程if (a[i] = b[j]&&1 <= k < j) dp[j] = dp[k] + 1需要注意的是格式控制。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int dp[1010],a[1010],b[1010];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m;
scanf("%d",&n);
for(int i=0; i<n; i++)
scanf("%d",&a[i]);
scanf("%d",&m);
for(int i=0; i<m; i++)
scanf("%d",&b[i]);
memset(dp,0,sizeof(dp));
int sum;
for(int i=0; i<n; i++)
{
sum=0;
for(int j=0; j<m; j++)
{
if(a[i]>b[j]&&sum<dp[j])
sum=dp[j];
if(a[i]==b[j])
dp[j]=sum+1;
}
}
int ans=0;
for(int i=0; i<m; i++)
if(ans<dp[i])
ans=dp[i];
printf("%d\n",ans);
if(t)
printf("\n");
}
return 0;
}