题目链接: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的更多相关文章

  1. uva10635 LCS映射转LIS

    题目给定 2个序列,要我们求LCS,但是序列的长度最长是250*250, LCS的时间复杂度是O(N*N),所以无法解决 我们可以第一个序列的数字,按位置,映射为1.2.3.4.5.6.7.8.9 那 ...

  2. UVa10635 - Prince and Princess(LCS转LIS)

    题目大意 有两个长度分别为p+1和q+1的序列,每个序列中的各个元素互不相同,且都是1~n^2之间的整数.两个序列的第一个元素均为1.求出A和B的最长公共子序列长度. 题解 这个是大白书上的例题,不过 ...

  3. LCS 的 NlogN作法

    这个算法其实是因为LIS有NlogN的作法,把LCS转化为了LIS来做. 对于序列A{},B{},我们可以记录下来B中的每个元素在A中出现的位置,按顺序保存在一个新序列当中, 如果有多个位置按倒序写, ...

  4. 我的第一篇博客----LCS学习笔记

    LCS引论 在这篇博文中,博主要给大家讲一个算法----最长公共子序列(LCS)算法.我最初接触这个算法是在高中学信息学竞赛的时候.那时候花了好长时间理解这个算法.老师经常说,这种算法是母算法,即从这 ...

  5. uva10635 LIS

    Prince and PrincessInput: Standard Input Output: Standard Output Time Limit: 3 Seconds In an n x n c ...

  6. 动态规划之最长公共子序列(LCS)

    转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...

  7. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  8. 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 ...

  9. UVA 11404 Palindromic Subsequence[DP LCS 打印]

    UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...

随机推荐

  1. 【leetcode】Number of 1 Bits (easy)

    做太多遍了,秒杀. class Solution { public: int hammingWeight(uint32_t n) { ; ), num++); return num; } };

  2. 【leetcode】Happy Number(easy)

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  3. 【python】An Introduction to Interactive Programming in Python(week two)

    This is a note for https://class.coursera.org/interactivepython-005 In week two, I have learned: 1.e ...

  4. 读取Spring的配置文件applicationContext.xml的5种方法

    1.利用ClassPathXmlApplicationContext,这种方式配置文件应该放在类包同路径下Java代码: ApplicationContext ct=new ClassPathXmlA ...

  5. 浅析_tmain()与main()的区别

    转自http://www.jb51.net/article/34516.htm _tmain()是为了支持unicode所使用的main一个别名,既然是别名,应该有宏定义过的,在哪里定义的呢?就在那个 ...

  6. 二、JavaScript语言--JS基础--JavaScript进阶篇--选项卡切换效果

    利用JavaScript知识,实现选项卡切换的效果. 效果图: 文字素材: 房产: 275万购昌平邻铁三居 总价20万买一居     200万内购五环三居 140万安家东三环     北京首现零首付楼 ...

  7. Clr Via C#读书笔记---CLR寄宿和应用程序域

    #1 CLR寄宿: 开发CLR时,Microsoft实际是将他实现成包含在一个dll中的COM服务器.Microsoft为CLR定义了一个标准的COM接口,并为该接口和COM服务器分配了GUID.安装 ...

  8. C#集合类型

    using System; using System.Collections; using System.Collections.Generic; namespace codeTest { class ...

  9. SQL exists( select 1 from

    use UnlockIndustry select * from Info_Coordinate as A join Info_Employee on A.EmployeeId=Info_Employ ...

  10. Jquery 自定义弹窗等待

    (function ($) { $.extend({ //弹窗蒙层 ShowLoadDialog : function () { ) { var cusrtxt = $("<div i ...