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

HDUOJ ---1423 Greatest Common Increasing Subsequence(LCS)

2014-03-29 20:50 381 查看

Greatest Common Increasing Subsequence

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


[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

[align=left]Source[/align]
ACM暑期集训队练习赛(二)

代码:动态规划求最长增长公共序列 下面展示的是压缩空间的lcs,由于不需要记录顺序,所以这样写,较为简便,如果要记录路径只需要将lcs[]--->换成lcs[][],
然后maxc,变为lcs[][]的上一行即可!

//增长lcs algorithm
#include<stdio.h>
#include<string.h>
#define maxn 505
int aa[maxn],bb[maxn];
int lcs[maxn];
int main()
{
int test,na,nb,i,j,maxc,res;
scanf("%d",&test);
while(test--)
{
scanf("%d",&na);
for(i=1;i<=na;i++)
scanf("%d",aa+i);
scanf("%d",&nb);
for(j=1;j<=nb;j++)
scanf("%d",bb+j);
memset(lcs,0,sizeof(lcs));
for(i=1;i<=na;i++)
{
maxc=0;
for(j=1;j<=nb;j++)
{
if(aa[i]==bb[j]&&lcs[j]<maxc+1)
lcs[j]=maxc+1;
if(aa[i]>bb[j]&&maxc<lcs[j])
maxc=lcs[j];
}
}
res=0;
for(i=1;i<=nb;i++)
if(res<lcs[i])res=lcs[i];
printf("%d\n",res);
if(test) putchar(10);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: