Uva10635 LCS
题目链接:http://vjudge.net/contest/137498#problem/G
题意:有两个长度为p+1和q+1的序列,每个序列的中的各个元素互不相同,且都是1~n^2之间的整。两个序列的第一个元素均为1。求出A和B的最长公共子序列长度。
输入: T //测试组数
n p q
A : p+1个数
B:q+1个数
输出:Case t: len
思路:因为n*n=62500 LCS的算法 O(pq)很慢,而且数组也开不下,所以可以转化为求LIS(O(nlogn))。
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=;
const int INF=; int s[maxn],dp[maxn];
int num[maxn]; /// num[x]为整数x的新编号,num[x]=0表示x没有在A中出现过 int main()
{
int T;
scanf("%d",&T);
for(int t=; t<=T; t++)
{
int N,p,q,x;
scanf("%d%d%d",&N,&p,&q);
memset(num,,sizeof(num));
for(int i=; i<=p+; i++)
{
scanf("%d",&x);
num[x]=i;
}
int n=;
for(int i=; i<q+; i++) ///转化为求s的LIS
{
scanf("%d",&x);
if(num[x])
s[++n]=num[x];
}
int len=;
dp[]=s[];
for(int i=; i<=n; i++)
{
if(s[i]>dp[len]) dp[++len]=s[i];
else dp[upper_bound(dp+,dp++len,s[i])-dp]=s[i];
}
printf("Case %d: %d\n",t,len);
}
return ;
}
Uva10635 LCS的更多相关文章
- uva10635 LCS映射转LIS
题目给定 2个序列,要我们求LCS,但是序列的长度最长是250*250, LCS的时间复杂度是O(N*N),所以无法解决 我们可以第一个序列的数字,按位置,映射为1.2.3.4.5.6.7.8.9 那 ...
- UVa10635 - Prince and Princess(LCS转LIS)
题目大意 有两个长度分别为p+1和q+1的序列,每个序列中的各个元素互不相同,且都是1~n^2之间的整数.两个序列的第一个元素均为1.求出A和B的最长公共子序列长度. 题解 这个是大白书上的例题,不过 ...
- LCS 的 NlogN作法
这个算法其实是因为LIS有NlogN的作法,把LCS转化为了LIS来做. 对于序列A{},B{},我们可以记录下来B中的每个元素在A中出现的位置,按顺序保存在一个新序列当中, 如果有多个位置按倒序写, ...
- 我的第一篇博客----LCS学习笔记
LCS引论 在这篇博文中,博主要给大家讲一个算法----最长公共子序列(LCS)算法.我最初接触这个算法是在高中学信息学竞赛的时候.那时候花了好长时间理解这个算法.老师经常说,这种算法是母算法,即从这 ...
- uva10635 LIS
Prince and PrincessInput: Standard Input Output: Standard Output Time Limit: 3 Seconds In an n x n c ...
- 动态规划之最长公共子序列(LCS)
转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- Hackerrank11 LCS Returns 枚举+LCS
Given two strings, a and , b find and print the total number of ways to insert a character at any p ...
- UVA 11404 Palindromic Subsequence[DP LCS 打印]
UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...
随机推荐
- 括号配对问题_栈<stack>
问题 A: 括号配对问题 时间限制: 3 Sec 内存限制: 128 MB提交: 3 解决: 2[提交][状态][讨论版] 题目描述 现在,有一行括号序列,请你检查这行括号是否配对. 输入 第一行 ...
- 【leetcode】Minimum Size Subarray Sum(middle)
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- UIColor+Hex
#import <UIKit/UIKit.h> @interface UIColor (Hex) + (UIColor *)colorWithHex:(long)hexColor;+ (U ...
- mybatis配置问题
//当构造函数有多个参数时,可以使用constructor-arg标签的index属性,index属性的值从0开始. <bean id="sqlSession" class= ...
- hdu 1249 三角形
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1249 part=3*s*(s-1)+2 #include<stdio.h> #includ ...
- jq 轮播图
<style> #focus{width:500px;height:200px;overflow:hidden;/*用一个div把图片包含设置超出范围隐藏*/} </style> ...
- 14.模板方法模式(Template Method)
using System; namespace ConsoleApplication7 { class Program { /// <summary> /// 模板方法模式——在一个抽象类 ...
- 在ubuntu上搭建开发环境9---Ubuntu删除ibus出现的问题及解决
删除 ibus输入法: sudo apt-get install ibus 我们会遇到下面的问题 Ubuntu 14.04 系统设置很多选项消失. 其实遇到这个问题的一个最主要的原因是之前执行过卸载i ...
- 【翻译二十一】java-并发之分拆和合并
Fork/Join This section was updated to reflect features and conventions of the upcoming Java SE 8 rel ...
- windows操作技巧
-------------------win7专有------------------- WIN+上下左右可以将当前窗口 最大化.左侧放置.右侧放置.最小化 WIN+Shift+左右是让窗口在多个屏幕 ...