[题解]UVa 10635 Prince and Princess


讲一下题目大意,就是有两个长度为p + 1和q + 1的序列,求它们的LCS。
如果用O(pq)的算法对于这道题来说还是太慢了。所以要另外想一些方法。注意到序列中的所有元素都不相同,所以两个序列中数对应的位置都是唯一的,就用第一个序列的元素对第二个序列的元素进行重新编号,记录它们在第一个序列中出现的位置(如果不存在就随便记一个不能达到的值),不存在的话就说明它们对LCS没有贡献。那么看张图:

如果不能明白,那。。看张有关不合法情况的图:

有没有发现LCS的长度就是第二个序列的LIS的长度?
/**
* uva
* Problem#10635
* Accepted
* Time:0ms
*/
#include<iostream>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<sstream>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<vector>
#include<stack>
using namespace std;
typedef bool boolean;
#define INF 0xfffffff
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
template<typename T>
inline void readInteger(T& u){
char x;
int aFlag = ;
while(!isdigit((x = getchar())) && x != '-');
if(x == '-'){
x = getchar();
aFlag = -;
}
for(u = x - ''; isdigit((x = getchar())); u = (u << ) + (u << ) + x - '');
ungetc(x, stdin);
u *= aFlag;
} template<typename T>
class IndexedStack{
public:
T *p;
int s;
IndexedStack():s(), p(NULL){ }
IndexedStack(int size):s(){
p = new T[(const int)size];
}
boolean empty() { return s == ; }
T top() { return p[s - ]; }
int size() { return s; }
void pop() { s--; }
void push(T& x) { p[s++] = x; }
void clear() { s = ; }
T& operator [](int pos) { return p[pos]; }
}; int n, p, q;
int *pce;
int *pss;
int *ets; inline void init(){
readInteger(n);
readInteger(p);
readInteger(q);
pce = new int[(const int)(p + )];
pss = new int[(const int)(q + )];
ets = new int[(const int)(n * n + )];
memset(ets, , sizeof(int) * (n * n + ));
p += , q += ;
for(int i = ; i <= p; i++){
readInteger(pce[i]);
ets[pce[i]] = i;
}
for(int i = ; i <= q; i++){
readInteger(pss[i]);
pss[i] = ets[pss[i]];
}
delete[] ets;
} int upper_bound(int *a, int from, int end, int val){
int l = from, r = end - ;
while(l <= r){
int mid = (l + r) >> ;
if(val < a[mid]) r = mid - ;
else l = mid + ;
}
return r + ;
} IndexedStack<int> s;
inline int lis(){
s = IndexedStack<int>(q + );
for(int i = ; i <= q; i++){
if(pss[i] == ) continue;
int l = upper_bound(s.p, , s.size(), pss[i]);
if(l == s.size()) s.push(pss[i]);
else s[l] = pss[i];
}
return s.size();
} int T, kase;
inline void solve(){
int len = lis();
printf("Case %d: %d\n", kase, len);
delete[] pss;
delete[] pce;
} int main(){
readInteger(T);
while(T--){
kase++;
init();
solve();
}
return ;
}
[题解]UVa 10635 Prince and Princess的更多相关文章
- uva 10635 - Prince and Princess(LCS)
题目连接:10635 - Prince and Princess 题目大意:给出n, m, k,求两个长度分别为m + 1 和 k + 1且由1~n * n组成的序列的最长公共子序列长的. 解题思路: ...
- UVA - 10635 Prince and Princess LCS转LIS
题目链接: http://bak.vjudge.net/problem/UVA-10635 Prince and Princess Time Limit: 3000MS 题意 给你两个数组,求他们的最 ...
- UVa 10635 Prince and Princess - 动态规划
讲一下题目大意,就是有两个长度为p + 1和q + 1的序列,求它们的LCS. 如果用O(pq)的算法对于这道题来说还是太慢了.所以要另外想一些方法.注意到序列中的所有元素都不相同,所以两个序列中数对 ...
- Uva 10635 - Prince and Princess 问题转化,元素互不相同(在自身序列中独特)的两个数列的LCS,LIS 难度: 2
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- [UVA 10635] Prince ans Princess
图片加载可能有点慢,请跳过题面先看题解,谢谢 这道题... 还是要点思维的... 第一眼看是个最长公共子序列,但是, \(N\le 62500\) ,并不能 \(O(n^2)\) 求 $ $ 这道题有 ...
- UVA 10635 Prince and Princess【LCS 问题转换为 LIS】
题目链接: http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=19051 题意: 有两个长度分别为p+1和q+1的由1到n2 ...
- UVA 10635 - Prince and Princess LCS转化为LIS
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- Uva 10635 - Prince and Princess LCS/LIS
两个长度分别为p+1和q+1的由1到n2之前的整数组成的序列,每个序列的元素各不相等,两个序列第一个元素均为1.求两个序列的最长公共子序列 https://uva.onlinejudge.org/in ...
- UVA 10635 Prince and Princess
题意描述:有两个长度分别为p+1和q+1的序列,每个元素中的各个元素互不相同.都是1~n^2之间的整数,求A和B的最长公共子序列.(2<=n<=250,1<=p,q<=n^2) ...
随机推荐
- 转载:使用命令行启动VirtualBox虚拟机
使用命令行启动VirtualBox虚拟机 装上VirtualBox就琢磨着如何让它开机自动启动,又或者能够通过命令行的形式直接启动指定的虚拟机. 看了下VirtualBox的官方文档,发现有一个命令可 ...
- 获取本机MAC地址
<%@ page contentType="text/html; charset=GBK" %><html><head><meta htt ...
- 一加3,CM13蓝牙共享互联网 无效。
一加3准备把4G网络共享给魅族PRO5. 但在一加3的蓝牙设置里怎么勾选都无用. 最后发现在要在PRO5上设置才行. 1.在蓝牙列表中,点击带圈的感叹号. 2.选择“互联网访问”. - --
- putty连接ubuntu虚拟机缓慢问题的解决
vmware安装系统使用了ubuntu,安装后每次用PUTTY登录发现都到等很久,经过上网搜索,发现是Ubuntu安全机制导致的连接缓慢问题, 解决方法如下; 1. sudo vim /etc ...
- gerrit error: unpack failed: error Permission denied
gerrit服务器迁移后,clone和pull代码到本地,都没问题. 但是,push时,报错: 查看了下git版本库存储目录,发现git下版本库镜像文件owner都是root.因为之前安装的gerri ...
- Crt单元
一.调用单元例:uses crt; 二.清屏例:clrscr; 三.移动光标例:gotoxy(a,b);其中a表示列号,b表示行号 四.清行例:clreol;清除光标所在行光标上及以后的所有字符 五. ...
- Oracle死锁产生的原因和解决办法
如果有两个会话,每个会话都持有另一个会话想要的资源,此时就会发生死锁.用下面实验来说明死锁的产生原因和解决办法.SESSION1:SQL> create table t2 as select * ...
- 关于ILDASM.EXE的知识整理
因为现在用的VS2010,发现,这个工具自己就带着ILDASM.EXE这个反编译工具 具体的查找方式为: C:\Program Files\Microsoft SDKS\Windows\V7.0\bi ...
- 关于Reflow回流
在CSS规范中有一个渲染对象的概念,通常用一个盒子(box, rectangle)来表示.mozilla通过一个叫frame的对象对盒子进行操作.frame主要的动作有三个: 构造frame, 以建立 ...
- 关于xib文件和storyboard文件的那些事儿
在ios中,一般建议使用代码布局,因为使用代码布局,后期维护容易,拓展容易,并且可以实现动态加载很多数据,但是代码布局比较繁琐,不适合初学者.Xib布局或者Storyboard布局比较方便.下面介绍一 ...