(LCIS)最长公共上升子序列 ZOJ 2432

本文详细介绍了如何使用动态规划解决寻找两个序列的最长公共上升子序列的问题,并提供了具体的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Greatest Common Increasing Subsequence

 


 

Time Limit: 2 Seconds        Memory Limit: 65536 KB        Special Judge

 


You are given two sequences of integer numbers. Write a program to determine their common increasing subsequence of maximal 
possible length.

Sequence S1, S2, ..., SN of length N is called an increasing subsequence of a sequence A1, A2, ..., AM of length M if there exist 1 <= i1 < i2 < ...< iN <= M such that Sj = Aij for all 1 <= j <= N, and Sj < Sj+1 for all 1 <= j < N. 


Input

Each sequence is described with M - its length (1 <= M <= 500) and M integer numbers Ai (-2^31 <= Ai < 2^31) - the sequence itself.


Output

On the first line of the output 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.


This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.


Sample Input

1

5
1 4 2 5 -12
4
-12 1 2 4


Sample Output

2
1 4

 

题意:给出两串数字,要你输出他们的最长公共上升子序列。并且要输出这个序列是什么。

 

思路:具体的可以看http://wenku.baidu.com/link?url=5c_6_9rR4pAJ0u2P5AKfMLw5Z1aKoZrNcMLQ56DZ7SuZAlD-nBzMCzzoGDzwuSf194SSopoEFBzFAni5clemvpWxCr2NuBOo-DsqC_JBIDi

 

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<string.h>
using namespace std;
const int maxn = 1000 + 5;
#define LL long long
int n, m;
LL a[maxn], b[maxn];
int f[maxn][maxn];
int pre[maxn][maxn];

int main()
{
	int T; cin >> T;
	while (T--) {
		scanf("%d", &n);
		for (int i = 1; i <= n; ++i) scanf("%lld", a + i);
		scanf("%d", &m);
		for (int i = 1; i <= m; ++i) scanf("%lld", b + i);
		memset(pre, -1, sizeof(pre));
		memset(f, 0, sizeof(f));
		for (int i = 1; i <= n; ++i) {
			int v = 0, k = 0;
			for (int j = 1; j <= m; ++j) {
		//		pre[i][j] = pre[i - 1][j];
				f[i][j] = f[i - 1][j];
				if (a[i] > b[j] && v < f[i - 1][j]) v = f[i - 1][j], k = j;
				if (a[i] == b[j] && v + 1>f[i][j]) f[i][j] = v + 1, pre[i][j] = k;
			}
		}
		int k = 1;
		for (int i = 1; i <= m; ++i)
		if (f[n][i]>f[n][k]) k = i;
		printf("%d\n", f[n][k]);
		if (f[n][k] == 0) continue;
		int i = n;
		vector<int> ans;
		for (int i = n; i >= 1;--i) 
		if (pre[i][k] != -1) ans.push_back(a[i]), k = pre[i][k];
		printf("%d", ans.back());
		for (int i = ans.size() - 2; i >= 0; --i) printf(" %d", ans[i]);
		printf("\n");
	}
}


 

 

 

 

 

### 最长公共上升子序列问题的解决方案 最长公共上升子序列(Longest Common Increasing Subsequence, LCIS)问题是一个结合了最长公共子序列(LCS)和最长上升子序列(LIS)的问题。它要求找到两个序列的最长公共子序列,同时该子序列必须是严格递增的。 #### 动态规划解法 动态规划是一种高效的方法来解决此类问题。定义状态 `dp[i][j]` 表示以第一个序列的前 `i` 个元素和第二个序列的前 `j` 个元素为考虑范围时,最长公共上升子序列的长度[^1]。 ##### 状态转移方程 对于两个序列 `A` 和 `B`,其长度分别为 `n` 和 `m`,状态转移方程如下: - 如果 `A[i] == B[j]` 并且存在一个比 `A[i]` 小的值使得当前子序列递增,则更新 `dp[i][j]`。 - 否则,`dp[i][j]` 取决于之前的状态值。 具体实现中,可以引入一个辅助数组 `prev` 来记录每个位置的最优前驱,以便后续回溯出具体的子序列[^2]。 ```python def longest_common_increasing_subsequence(A, B): n, m = len(A), len(B) dp = [[0] * (m + 1) for _ in range(n + 1)] prev = [[-1] * (m + 1) for _ in range(n + 1)] for i in range(1, n + 1): for j in range(1, m + 1): if A[i - 1] == B[j - 1]: # 找到一个可能的递增点 max_len = 0 for k in range(j): if B[k - 1] < B[j - 1] and dp[i - 1][k] > max_len: max_len = dp[i - 1][k] dp[i][j] = max_len + 1 else: dp[i][j] = dp[i][j - 1] # 回溯找出具体子序列 result = [] max_len = 0 pos = 0 for j in range(1, m + 1): if dp[n][j] > max_len: max_len = dp[n][j] pos = j while pos != -1: result.append(B[pos - 1]) next_pos = prev[n][pos] pos = next_pos return result[::-1], max_len ``` #### 时间复杂度分析 上述算法的时间复杂度为 \(O(n \times m^2)\),其中 \(n\) 和 \(m\) 分别是两个序列的长度。这是因为内层循环需要遍历所有可能的前驱节点以确保递增性[^3]。 #### 示例代码 以下是一个简单的例子,展示如何使用上述函数: ```python A = [3, 4, 9, 1] B = [5, 3, 8, 9, 10, 2, 1] result, length = longest_common_increasing_subsequence(A, B) print("最长公共上升子序列:", result) print("长度:", length) ``` #### 输出结果 ``` 最长公共上升子序列: [3, 9] 长度: 2 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值