最长公共上升子序列

1401: Sherlock and Moriarty II

时间限制: 1 Sec   内存限制: 128 MB
提交: 89   解决: 31
[ 提交][ 状态][ 讨论版]

题目描述

Sherlock Holmes is an excellent detective and Moriarty is the big villain. One day Moriarty kidnaps Sherlock's
boyfriend ---- Jhon Watson. Of course,Sherlock goes to save him. As sherlock has found the password in the previous
problem.When he finds Jhon, there is a time bomb. Sherlock has to defuse the bomb.
The bomb has a special coded lock,the lock has two sequence A and B, Sherlock has to find a new Sequence C , C is
both the subsequence of A and B, and for each i<j C[i]<C[j]. The length of C should be as long as possible.The length
of sequence C is the password.

输入

There are several cases.
For each case,the first line is N and M (1<=n,m<=100)
And the is sequence A and B , length(A)=N length(B)=M (0<= A[i],B[i]<=50)

输出

The length of sequence C.

样例输入

5 5
1 2 0 4 5
1 0 4 5 2

样例输出

3

这是最复杂

#include<stdio.h>
#include<string.h>
struct node{ int max, len; };
int a[101];
int b[101];
node c[101][101];
int asize, bsize;
int main(){
	//freopen("in.txt", "r", stdin);
	while (~scanf("%d%d", &asize, &bsize)){
		int i, j,m,n;
		for (i = 0; i < asize; i++)
			scanf("%d", &a[i]);
		for (i = 0; i < bsize; i++)
			scanf("%d", &b[i]);
		memset(c, 0, sizeof(c));
		for (i = 0; i < asize;i++)
		for (j = 0; j < bsize; j++)
		{
			if (a[i] != b[j])continue;
			c[i][j].len = 1;
			c[i][j].max = a[i];
			for (m = 0; m < i;m++)
			for (n = 0; n < j; n++)
			if (c[m][n].max<a[i] && 1 + c[m][n].len>c[i][j].len)
				c[i][j].len = 1 + c[m][n].len;
		}
		m = 0;
		for (i = 0; i < asize;i++)
		for (j = 0; j < bsize; j++){
			if (c[i][j].len>m)m = c[i][j].len;
		}
		printf("%d\n", m);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值