讲一下题目大意,就是有两个长度为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 - 动态规划的更多相关文章

  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 LCS转LIS

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

  3. [题解]UVa 10635 Prince and Princess

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

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

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  5. UVA 10635 Prince and Princess【LCS 问题转换为 LIS】

    题目链接: http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=19051 题意: 有两个长度分别为p+1和q+1的由1到n2 ...

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

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

  7. Uva 10635 - Prince and Princess LCS/LIS

    两个长度分别为p+1和q+1的由1到n2之前的整数组成的序列,每个序列的元素各不相等,两个序列第一个元素均为1.求两个序列的最长公共子序列 https://uva.onlinejudge.org/in ...

  8. UVA 10635 Prince and Princess

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

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

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

随机推荐

  1. msc文件

    MSC微软管理控制台(Microsoft Management Control)文件.可以点击开始/运行,然后输入下列文件名就可以打开相应的控制窗口. 除第三个文件外,其他均在C:\WINDOWS\s ...

  2. 影响SEO 搜索引擎优化的网页制作细节

    1.将css与javascript全部用下边的方法分离到外部文件中去. [html]<link rel=”stylesheet” type=”text/css” href=”css/style. ...

  3. LoadRunner-录制脚本中文显示乱码

    录制的脚本中中文字符显示乱码 在Tools->Recording Options中设置支持UTF-8即可

  4. Mayor's posters---poj2528线段树、离散化

    题目链接:http://poj.org/problem?id=2528 题意:有n张海报要贴,每张需要用的区间为L到R,后面的可以贴在之前的上面,就是吧之前的挡住,求最后我们能看到几张海报: 我们可以 ...

  5. js-jquery-插件开发(一)

    jQuery插件开发模式 jQuery插件开发方式主要有三种:1.通过$.extend()来扩展jQuery 主要是在jQuery命名空间或者理解成jQuery身上添加了一个静态方法2.通过$.fn ...

  6. java-JProfiler(五)-监控性能

    原文地址:http://blog.csdn.net/chendc201/article/details/22897999 一.基础认识 1. 在Live Memory视图里右击相关类,选中Mark C ...

  7. MongoDB3.x中添加用户和权限控制

    现在需要创建一个帐号,该账号需要有grant权限,即:账号管理的授权权限.注意一点,帐号是跟着库走的,所以在指定库里授权,必须也在指定库里验证(auth) ? 1 2 3 4 5 6 7 8 9 10 ...

  8. IE加载项

    加载项   加载项也称为ActiveX控件.浏览器扩展.浏览器帮助应用程序对象或工具栏,可以通过提供多媒体或交互式内容(如动画)来增强对网站的体验. 但是,某些加载项可导致计算机停止响应或显示不需要的 ...

  9. [LeetCode] 285. Inorder Successor in BST_Medium tag: Inorder Traversal

    Given a binary search tree and a node in it, find the in-order successor of that node in the BST. No ...

  10. ACM 未解决的问题

    还没有搞定的ACM问题列表. google code jam Round1A Round1B Round1C Round2 Round3 Onsite Finals 百度之星 一.资格赛题目: dis ...