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

HDU 1423 Greatest Common Increasing Subsequence

2010-03-20 11:30 288 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1423

题目大意:就是叫你求最长公共递增子序列的长度。So就用LCIS算法做了。

#include <stdio.h>
#include <string.h>
const int size = 505;

int a[size],b[size];
int len[size];/*b字符串到第j个字符时,该字符位置处的公共递增因子数*/

int LCIS(int M,int N)
{
int locate,i,j,Max = -1;
memset(len,0,sizeof(len));
len[0] = -1;
for (i=1;i<=M;i++)
{
locate=0;
for (j=1;j<=N;j++)
{
/*当前要比较的数值为a[i],所以我们寻找b[j]中比a[i]小,但len[j]最大的值,找到了就用locate记录位置*/
if(b[j]<a[i]&&len[j]>len[locate])
locate = j;
if(b[j] == a[i])
len[j] = (len[locate]>=0?len[locate]:0)+1;/*len[locate]值+1就为当前字符的公共递增因子数*/
}
}
for(i=1;i<=N;i++)
if(len[i]>Max)
Max = len[i];
return Max;
}

int main()
{
int M,N,NumOfTest,i;
scanf("%d",&NumOfTest);
while (NumOfTest--)
{
scanf("%d",&M);
for(i=1;i<=M;i++)
scanf("%d",&a[i]);
scanf("%d",&N);
for(i=1;i<=N;i++)
scanf("%d",&b[i]);
printf("%d/n",LCIS(M,N));
if(NumOfTest)
printf("/n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: