Greatest Common Increasing Subsequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8374 Accepted Submission(s): 2703
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
最长公共上升子序列问题。在最长公共子序列的基础上加了一个递增的限制。那就写一个函数求当前点之前的最长递增子序列的长度,前面子序列的末端要小于当前点。其他的和最长公共子序列的求解类似。这一题有个坑点,就是格式问题,每两个输出之间有个空行。
代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<iostream>
using namespace std;
#define inf 1<<29
int n,m;
long long a[505],b[505],dp[505][505];
int find(int x,int y)
{
long long maxlen=0;
for(int i=0;i<x;i++)
{
for(int j=0;j<y;j++)
{
if(a[i]<a[x] && b[j]<b[y])
{
maxlen=max(maxlen,dp[i][j]);
}
}
}
return maxlen;
}
int main()
{
int t,flag=0;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%lld",&a[i]);
scanf("%d",&m);
for(int j=0;j<m;j++)
scanf("%lld",&b[j]);
long long maxlen=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(a[i]==b[j])
dp[i][j]=find(i,j)+1;
else
dp[i][j]=max(find(i-1,j),find(i,j-1));
maxlen=max(maxlen,dp[i][j]);
}
}
if(flag)
printf("\n");
flag=1;
printf("%lld\n",maxlen);
}
return 0;
}
本文介绍了一种改进的最长公共子序列问题——最长公共上升子序列问题,并提供了一个具体的解决算法实例。该问题是在两个整数序列中寻找最长的共同递增子序列。

886

被折叠的 条评论
为什么被折叠?



