hdu 1423 Greatest Common Increasing Subsequence 经典dp

// 最长公共上升子序列问题
// 以f[i][j]表示a串前i个字符与b串前j个字符并且以b[j]为结尾匹配的状况
// 为什么定义这个状态呢,因为我也是看到的百度文库中@我们都爱刘汝佳
// 这篇文章中的介绍,发现这个状态真的非常好用
// 状态转移方程
// a[i]!=b[j]那么f[i][j] = f[i-1][j],换而言之,我们可以直接丢掉a串中
// 的a[i]不用考虑,这里和最长公共子序列的问题有点类似
// a[i]==b[j]那么f[i][j] = max(f[i-1][k]+1){k<j&&b[k]<b[j]}
// a[i]已经和b[j]配对了,则在a[0...i-1]与b[0...j-1]找最大的+1就是我们要的
// 答案,我们的状态中有以b[j]为结尾这个定义,则我们只要找
// 当a[i]>b[j]时f[i-1][j]这样的max+1就可以啦,(因为这样的a[i]是不被需要的)
// 更加节省空间的做法就是用滚动数组
// 这算是dp的经典问题啦,继续练吧。。。
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define ceil(a,b) (((a)+(b)-1)/(b))
#define endl '\n'
#define gcd __gcd
#define highBit(x) (1ULL<<(63-__builtin_clzll(x)))
#define popCount __builtin_popcountll
typedef long long ll;
using namespace std;
const int MOD = 1000000007;
const long double PI = acos(-1.L);

template<class T> inline T lcm(const T& a, const T& b) { return a/gcd(a, b)*b; }
template<class T> inline T lowBit(const T& x) { return x&-x; }
template<class T> inline T maximize(T& a, const T& b) { return a=a<b?b:a; }
template<class T> inline T minimize(T& a, const T& b) { return a=a<b?a:b; }

const int maxn = 512;
int a[maxn];
int b[maxn];
int f[maxn][maxn];
int dp[maxn];
int n,m;
void init(){
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	scanf("%d",&m);
	for (int i=1;i<=m;i++)
		scanf("%d",&b[i]);
	memset(f,0,sizeof(f));
	memset(dp,0,sizeof(dp));
}

void solve(){

	int mx ;
	for (int i=1;i<=n;i++){
		mx = 0;
		for (int j=1;j<=m;j++){
			f[i][j] = f[i-1][j];
			if (a[i]>b[j])
				mx = max(mx,f[i][j]);
			if (a[i]==b[j])
				f[i][j] = mx+1;
		}
	}
	mx = -1;
	for (int i=1;i<=m;i++)
		mx = max (mx , f[n][i]);
	printf("%d\n",mx);
}
void solve1(){
	int mx;
	for (int i=1;i<=n;i++){
		mx = 0;
		for (int j=1;j<=m;j++){
			if (a[i]>b[j])
			mx = max(dp[j],mx);
			if (a[i]==b[j])
				dp[j] = mx+1;
		}
	}
	mx = 0;
	for (int i=1;i<=m;i++)
		mx = max(mx,dp[i]);
	printf("%d\n",mx);

}


int main() {
	int t;
    freopen("G:\\Code\\1.txt","r",stdin);
	scanf("%d",&t);
	while(t--){
		init();
		solve1();
		if (t)
			puts("");
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值