题目

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1576

题意

两个元素互不相同(在自身数列中互不相同)的数列,求二者的LCS。

思路

如刘书,

非常棒的思路:

1. 明显LCS问题只要关注两个数列的交集,利用第一个数列a的元素,对第二个数列b中的元素重编号(b元素中没有出现在a数列中的可以忽略)问题就转化为最长上升子序列问题。

2. 使用lower_bound不断维护数组leftNum,其中leftNum[i]是上升子序列延伸到i+1长度时最后一个元素(也是最大的那个元素,比如2,3,5中的5)的最小值

感想

1. 反射性地想用树状数组维护leftNum

代码

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#define LOCAL_DEBUG
using namespace std;
typedef pair<int, int> MyPair;
const int MAXN = * + ;
int b[MAXN];
int leftNum[MAXN]; int main() {
#ifdef LOCAL_DEBUG
freopen("C:\\Users\\Iris\\source\\repos\\ACM\\ACM\\input.txt", "r", stdin);
//freopen("C:\\Users\\Iris\\source\\repos\\ACM\\ACM\\output.txt", "w", stdout);
#endif // LOCAL_DEBUG
int T;
scanf("%d", &T);
for (int ti = ; ti <= T; ti++) {
int n, p, q;
scanf("%d%d%d", &n, &p, &q);
map<int, int> a2ind;
for (int i = ; i <= p; i++) {
int tmp;
scanf("%d", &tmp);
a2ind[tmp] = i;
}
int blen = ;
for (int i = ; i <= q; i++) {
int tmp;
scanf("%d", &tmp);
if (a2ind.count(tmp) != ) {
b[blen++] = a2ind[tmp];
}
}
int leftlen = ;
for (int i = ; i < blen; i++) {
int ind = lower_bound(leftNum, leftNum + leftlen, b[i]) - leftNum;
leftNum[ind] = b[i];
if (ind == leftlen)leftlen++;
}
printf("Case %d: %d\n", ti, leftlen);
} return ;
}

Uva 10635 - Prince and Princess 问题转化,元素互不相同(在自身序列中独特)的两个数列的LCS,LIS 难度: 2的更多相关文章

  1. uva 10635 - Prince and Princess(LCS)

    题目连接:10635 - Prince and Princess 题目大意:给出n, m, k,求两个长度分别为m + 1 和 k + 1且由1~n * n组成的序列的最长公共子序列长的. 解题思路: ...

  2. [题解]UVa 10635 Prince and Princess

    讲一下题目大意,就是有两个长度为p + 1和q + 1的序列,求它们的LCS. 如果用O(pq)的算法对于这道题来说还是太慢了.所以要另外想一些方法.注意到序列中的所有元素都不相同,所以两个序列中数对 ...

  3. UVa 10635 Prince and Princess - 动态规划

    讲一下题目大意,就是有两个长度为p + 1和q + 1的序列,求它们的LCS. 如果用O(pq)的算法对于这道题来说还是太慢了.所以要另外想一些方法.注意到序列中的所有元素都不相同,所以两个序列中数对 ...

  4. UVA - 10635 Prince and Princess LCS转LIS

    题目链接: http://bak.vjudge.net/problem/UVA-10635 Prince and Princess Time Limit: 3000MS 题意 给你两个数组,求他们的最 ...

  5. UVA 10635 - Prince and Princess LCS转化为LIS

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  6. UVA 10635 Prince and Princess

    题意描述:有两个长度分别为p+1和q+1的序列,每个元素中的各个元素互不相同.都是1~n^2之间的整数,求A和B的最长公共子序列.(2<=n<=250,1<=p,q<=n^2) ...

  7. uva 10635 Prince and Princess(LCS成问题LIS问题O(nlogn))

    标题效果:有两个长度p+1和q+1该序列.的各种元素的每个序列不是相互同.并1~n^2之间的整数.个序列的第一个元素均为1. 求出A和B的最长公共子序列长度. 分析:本题是LCS问题,可是p*q< ...

  8. [UVA 10635] Prince ans Princess

    图片加载可能有点慢,请跳过题面先看题解,谢谢 这道题... 还是要点思维的... 第一眼看是个最长公共子序列,但是, \(N\le 62500\) ,并不能 \(O(n^2)\) 求 $ $ 这道题有 ...

  9. UVA - 10635 Prince and Princess(LCS,可转化为LIS)

    题意:有两个长度分别为p+1和q+1的序列,每个序列中的各个元素互不相同,且都是1~n2的整数.两个序列的第一个元素均为1.求出A和B的最长公共子序列长度. 分析: A = {1,7,5,4,8,3, ...

随机推荐

  1. TypeError: add() argument after * must be an iterable, not Settings的错误原因

    在抄代码的时候发现有个错误: TypeError: add() argument after * must be an iterable, not Settings 看不懂,百度才知道原因,原来是第2 ...

  2. nginx 启动报错 1113: No mapping for the Unicode character exists in the target multi-byte code

    failed (1113: No mapping for the Unicode character exists in the target multi-byte code page) 因为路径有中 ...

  3. 学习笔记54—均方误差(MSE)和均方根误差(RMSE)和平均绝对误差(MAE)

    https://blog.csdn.net/reallocing1/article/details/56292877 MSE: Mean Squared Error  均方误差是指参数估计值与参数真值 ...

  4. d3 .each()

    d3.select("xxx") .each(function (d) { //this表示当前element 而 d表示绑定的数据 something(this); }); 注意 ...

  5. MYSQL常用函数(类型转化函数)

    为了进行数据类型转化,MySQL提供了CAST()函数,它可以把一个值转化为指定的数据类型.类型有:BINARY,CHAR,DATE,TIME,DATETIME,SIGNED,UNSIGNED 示例: ...

  6. Practical Vim 第一章 & 第二章

    第一章:Vim 解决问题的方式 前言 本质上讲,我们的工作是重复性的.凡是可以简化重复性操作的方式,都会成倍地节省我们的时间. Vim 对重复性操作进行了优化.它之所以能高效地重复,是因为它会记录我们 ...

  7. JS过渡和变形效果演示(举例:鼠标滑过图片放大) --JS案例

    1.代码: <!DOCTYPE html><html><head> <meta charset="utf-8"> <title ...

  8. 配置samba 服务器 共享Linux目录

    配置samba 服务器 共享Linux目录 1.安装: yum install -y samba* 2.修改配置文件 vim /etc/samba/smb.conf [web] path = /usr ...

  9. mysql find_in_set()函数的使用

    mysql 中 find_in_set() 函数语法: FIND_IN_SET(str,strList) str 要查询的字符串 strList 字段名,参数以“,”分隔,如(1,2,6,8) 查询字 ...

  10. [pat]数素数

    时间限制:1000 ms 内存限制 32768 KB 代码长度限制 100 KB 判断程序 Standard (来自 小小) 题目描述 令Pi表示第i个素数.现任给两个正整数M <= N < ...