您的位置:首页 > 产品设计 > UI/UE

hdu 1423 Greatest Common Increasing Subsequence(最长公共递增子序列lcis)

2013-04-05 13:53 453 查看
[align=left]http://acm.hdu.edu.cn/showproblem.php?pid=1423[/align]

[align=left]Problem Description[/align]
This is a problem from ZOJ 2432.To make it easyer,you just need output the length of the subsequence.
[align=left]Input[/align]
Each sequence is described with M - its length (1 <= M <= 500) and M integer numbers Ai (-2^31 <= Ai < 2^31) - the sequence itself.
[align=left]Output[/align]
output print L - the length of the greatest common increasing subsequence of both sequences.

[align=left]Sample Input[/align]

1
5
1 4 2 5 -12
4
-12 1 2 4

[align=left]Sample Output[/align]

2

lcis:dp

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<string.h>
#include<string>
#include<ctime>
#include<queue>
#include<list>
#include<map>
#define INF 9999999
#define MAXN 10000
using namespace std;
//    priority_queue<int,vector<int>,greater<int> > pq;

int dp[550];//b数组到第j个数字时,该数字位置处的公共递增因子数

int main()
{
int t;
cin>>t;
while(t--)
{
int n,m;
cin>>n;
int i,j,a[550],b[550];
for(i=0;i<n;i++)
cin>>a[i];
cin>>m;
for(i=0;i<m;i++)
cin>>b[i];
memset(dp,0,sizeof(dp));
//    dp[0]=-1;
for(i=0;i<n;i++)
{
int k=0;
for(j=0;j<m;j++)
{
//当前要比较的数值为a[i],所以我们寻找b[j]中比a[i]小,但dp[j]最大的值,找到了就用k记录位置
if(a[i]>b[j]&&dp[j]>dp[k])
k=j;
if(a[i]==b[j])
dp[j]=dp[k]+1;  //dp[k]值+1就为当前字符的公共递增因子数
}
}
int maks=-1;
for(i=0;i<m;i++)
if(maks<dp[i])
maks=dp[i];
cout<<maks<<endl;
if(t)
cout<<endl;
}
return 0;
}


对与测试数据:
1
5
1 4 2 5 -12
4
-12 1 2 4

dp中的数据变化如下:
-12 1 2 4
1 0 1 0 0
4 0 1 0 2
2 0 1 2 2
5 0 1 2 2
-12 1 1 2 2

最后dp中的数据变成了 1 1 2 2
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐