POJ - 2127 Greatest Common Increasing Subsequence(线性dp,最长公共上升子序列)

该博客介绍了如何求解两个整数序列的最大公共上升子序列(LCIS)问题,使用线性动态规划(DP)方法。通过分析决策集合的变化,优化了转移方程,降低了算法复杂度。给出了样例输入和输出,并指出当决策集合只增不减时,可以使用变量记录避免重复扫描,提高效率。
摘要由CSDN通过智能技术生成

POJ - 2127 Greatest Common Increasing Subsequence
You are given two sequences of integer numbers. Write a program to determine their common increasing subsequence of maximal possible length.
Sequence S 1 , S 2 , . . . , S N of length N is called an increasing subsequence of a sequence A 1 , A 2 , . . . , A M of length M if there exist 1 <= i 1 < i 2 < . . . < i N <= M such that S j = A ij for all 1 <= j <= N , and S j < S j+1 for all 1 <= j < N .
Input
Each sequence is described with M — its length (1 <= M <= 500) and M integer numbers A i (-2 31 <= A i < 2 31 ) — the sequence itself.
Output
On the first line of the output file print L — the length of the greatest common increasing subsequence of both sequences. On the second line print the subsequence itself. If there are several possible answers, output any of them.
Sample Input
5
1 4 2 5 -12
4
-12 1 2 4
Sample Output
2
1 4
题意:求最长公共上升子序列,记录方案,任意输出。
这道题转移部分的优化告诉我们,在实现状态转移方程时,要注意观察决策集合的范围随着状态变化情况。对于
决策集合中的元素只增多不减少的情景,就可以像本题一样维护一个变量来记录决策集合的当前信息,避免重复扫描,
把转移的复杂度降低一个量级。

F(i,j)表示a序列和b序列,其中a1—ai与b1—bj以bj结尾的LCIS的长度。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#define ll long long
#define llu unsigned long long
using namespace std;
const int maxn=550;
int dp[maxn][maxn];
int wa
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值