uva10635 Prince and Princess LCS 变 lIS

本文介绍如何将UVA10635王子公主问题从最长公共子序列(LCS)问题转化为最长递增子序列(LIS)问题。通过对两个序列进行特殊处理,降低时间复杂度到nlogn,使得问题得以解决。
// uva10635 Prince and Princess LCS 变 lIS
// 本意求LCS,但是规模有60000多,复杂度肯定不够
// 注意如果俩个序列的值的范围相同,那么可以在一个
// 串中记录在另外一个串中的位置。这样就可以转化成
// 最长上升子序列的问题啦,复杂度是nlogn,可以ac
// 这题,还是挺有考究的价值的,很不错
// 哎,继续练吧。。。。。

#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 = 352 * 352;
int a[maxn];
int b[maxn];
int n,p,q;
int cnt;
int dp[maxn];
const int inf = 0x3f3f3f3f;

void print(int a[],int p){
	for (int i=1;i<=p;i++)
		printf("%d ",a[i]);
	puts("");
}

void init(){
	scanf("%d%d%d",&n,&p,&q);
	memset(dp,inf,sizeof(dp));
	memset(b,0,sizeof(b));
	scanf("%d",&a[0]);
	int x;
	for (int i=1;i<=p;i++){
		scanf("%d",&x);
		a[x] = i;
	}
	scanf("%d",&b[0]);
	for (int i=1;i<=q;i++){
		scanf("%d",&x);
		b[i] = a[x];
	}
//	print(a,p);
//	print(b,q);
	cnt = 0;
	for (int i=1;i<=q;i++){
		if (b[i]){
			a[cnt++] = b[i];
		}
	}
	//print(a,p);
}

void solve(){
	for (int i=0;i<cnt;i++){
		*lower_bound(dp,dp+cnt,a[i]) = a[i];
	}
	int k = lower_bound(dp,dp+cnt,inf) - dp;
	printf("%d\n",k+1);
}

int main() {
	int t;
	//freopen("G:\\Code\\1.txt","r",stdin);
	scanf("%d",&t);
	int kase = 1;
	while(t--){
		init();
		printf("Case %d: ",kase++);
		solve();
	}
	return 0;
}

LCSLIS 的核心思想是:**将两个序列的最长公共子序列问题转化为一个序列的最长递增子序列问题**。但这种转换并不是在所有情况下都适用,它有**严格的前提条件**。 --- ## ✅ LCSLIS 的前提条件: ### **条件 1:其中一个序列的元素是 1 到 n 的排列(即所有元素互不重复,且覆盖 1~n)** 也就是说,**该序列中每个数从 1 到 n 各出现一次**。 通常我们选择这个序列作为 `b` 序列,然后: - 将 `b` 中每个元素的位置记录下来 - 把 `a` 中的元素转换为它们在 `b` 中的位置 - 然后对这个新序列求 LIS,其长度就是 LCS 的长度 --- ## ✅ 举例说明: ### 给定: ``` a = [1, 7, 3, 5, 9, 4, 8] b = [1, 3, 5, 4, 8, 6, 7] ``` - `b` 是 1~7 的排列(所有元素唯一) - 我们记录每个元素在 `b` 中的位置: ``` pos[1] = 1 pos[3] = 2 pos[5] = 3 pos[4] = 4 pos[8] = 5 pos[6] = 6 pos[7] = 7 ``` - 遍历 `a`,将其中存在于 `b` 的元素替换为它们在 `b` 中的位置: ``` a -> [1, 7, 3, 5, 9, 4, 8] 替换为 -> [1, 7, 2, 3, 无效, 4, 5] 过滤无效后得到:[1, 7, 2, 3, 4, 5] ``` - 对这个新序列求 LIS: ``` LIS = [1, 2, 3, 4, 5] → 长度为 5 ``` → 所以 LCS 的长度也为 5 --- ## ✅ 条件 2:另一个序列可以有重复元素,但我们只取其在 `b` 中出现的部分 也就是说,**只要 `b` 是排列,即使 `a` 中有重复元素,也可以进行转换**。 --- ## ✅ 为什么必须是排列? 因为 LIS 要求的是**严格递增**的子序列。 如果我们把 `a` 中的元素映射为它们在 `b` 中的位置,那么: - 只有当这些位置是唯一的、递增的,才能保证对应的是 `b` 中的公共子序列 - 如果 `b` 中有重复元素,那么一个元素可能对应多个位置,无法确定唯一映射,转换失败 --- ## ✅ 总结: | 前提条件 | 说明 | |----------|------| | `b` 必须是一个排列 | 所有元素唯一,且为 1~n | | `a` 可以有重复元素 | 但只取在 `b` 中存在的部分 | | 映射后的序列求 LIS | LIS 的长度 = LCS 的长度 | ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值