UVA - 10635 Prince and Princess LCS转LIS
题目链接:
http://bak.vjudge.net/problem/UVA-10635
Prince and Princess
Time Limit: 3000MS
题意
给你两个数组,求他们的最长公共子串。
题解
每个数组的大小最大有62500,所以n^2的经典算法肯定行不通。
那么,我么就需要找突破口:这题和一般的最长公共子串问题有什么不同,题目告诉我们每个数字只出现一次。明显要在这上面做文章!由于每个数位子固定了,所以匹配是唯一的,(pos1,pos2)表示数x在a数组中的位置,在b数组中的位置。我们按顺序处理出所有的这样的顶点对。然后题目就转换成了二维的最长上升子串问题了。
代码
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#include<sstream>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII;
const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0);
//start----------------------------------------------------------------------
const int maxn=255*255;
int mp[maxn];
int a[maxn],b[maxn];
int arr[maxn];
int main() {
int tc,kase=0;
int n,p,q;
scf("%d",&tc);
while(tc--){
scf("%d%d%d",&n,&p,&q);
p++,q++;
clr(mp,-1);
for(int i=1;i<=p;i++) scf("%d",&a[i]);
for(int i=1;i<=q;i++){
scf("%d",&b[i]);
mp[b[i]]=i;
}
int tot=0;
for(int i=1;i<=p;i++){
if(mp[a[i]]>=0){
arr[++tot]=mp[a[i]];
}
}
// for(int i=1;i<=tot;i++) prf("%d\n",arr[i]);
vector<int> ra;
for(int i=1;i<=tot;i++){
int pos=upper_bound(all(ra),arr[i])-ra.begin();
if(pos==ra.sz()){
ra.pb(arr[i]);
}else{
ra[pos]=arr[i];
}
}
prf("Case %d: %d\n",++kase,ra.sz());
}
return 0;
}
//end-----------------------------------------------------------------------
UVA - 10635 Prince and Princess LCS转LIS的更多相关文章
- 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://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【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问题O(nlogn))
标题效果:有两个长度p+1和q+1该序列.的各种元素的每个序列不是相互同.并1~n^2之间的整数.个序列的第一个元素均为1. 求出A和B的最长公共子序列长度. 分析:本题是LCS问题,可是p*q< ...
- 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 and Princess (LCS变形LIS)
直接LCS是时间复杂度是O(p*q)的,但是序列元素各不相同,只要把其中一个序列映射成有序的, 另外一个序列再做相同的映射,没有的直接删掉,就变成了求另一个序列LIS. #include<bit ...
- UVA - 10635 Prince and Princess(LCS,可转化为LIS)
题意:有两个长度分别为p+1和q+1的序列,每个序列中的各个元素互不相同,且都是1~n2的整数.两个序列的第一个元素均为1.求出A和B的最长公共子序列长度. 分析: A = {1,7,5,4,8,3, ...
- UVA 10635 Prince and Princess—— 求LCS(最长公共子序列)转换成 求LIS(最长递增子序列)
题目大意:有n*n个方格,王子有一条走法,依次经过m个格子,公主有一种走法,依次经过n个格子(不会重复走),问他们删去一些步数后,重叠步数的最大值. 显然是一个LCS,我一看到就高高兴兴的打了个板子上 ...
随机推荐
- NRF52832初步使用
开发环境搭建 开发环境涉及到协议栈SDK版本.keil PACK版本的匹配问题,目前测试通过的环境如下: windows系统:win10 硬件:NRF52832测试板.JLINK-V8仿真器 Keil ...
- Verilog_Day2_Plus
内容为书中第4章 等式运算符 “==” 与 “===” 的区别 Verilog中存在4种等式运算符: == (等于); != (不等于); === (等于); !== (不等于). “==”与&quo ...
- IntelliJ IDEA 添加jar包
事先下载完成需要的javacsv.jar包. java项目在没有导入该jar包之前,显示如下图所示 点击 File -> Project Structure(快捷键 Ctrl + Al ...
- WPF DataGrid 样式分享
原文:WPF DataGrid 样式分享 隔行换色,鼠标单击,悬浮样式都有 先看效果: 代码: <DataGrid AutoGenerateColumns="False" N ...
- RabbitMQ(一):Window安装RabbitMQ
原文:RabbitMQ(一):Window安装RabbitMQ 1.安装ERLANG语言环境 由于RabbitMQ是采用Erlang编写的,因此我们需要先安装该语言库,以便运行代理服务器.从Erlan ...
- 4-[HTML]-body常用标签1
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 【LG3247】[HNOI2016]最小公倍数
[LG3247][HNOI2016]最小公倍数 题面 洛谷 题解 50pts 因为拼凑起来的部分分比较多,所以就放一起了. 以下设询问的\(a,b\)为\(A,B\), 复杂度\(O(nm)\)的:将 ...
- 【BZOJ 2132】圈地计划 && 【7.22Test】计划
两种版本的题面 Description 最近房地产商GDOI(Group of Dumbbells Or Idiots)从NOI(Nuts Old Idiots)手中得到了一块开发土地.据了解,这块土 ...
- java多线程的简单demo
模拟场景:顾客买车从车库中取车,厂家生产车,车存储在车库中.买家.厂家对同一个车库中的车操作 一.不加同步机制的代码如下: package com.joysuch.testng.thread; imp ...
- Jmeter接口测试(八)cookie设置
HTTP Cookie 管理器 如果你有一个 HTTP 请求,其返回结果里包含一个 cookie,那么 使用 JmeterCookie 管理器会自动将该 cookie保存起来,而且以后所有对该网站的请 ...