See LCS again
时间限制:1000 ms | 内存限制:65535 KB
难度:3

描述
There are A, B two sequences, the number of elements in the sequence is n、m;

Each element in the sequence are different and less than 100000.

Calculate the length of the longest common subsequence of A and B.

输入
The input has multicases.Each test case consists of three lines;
The first line consist two integers n, m (1 < = n, m < = 100000);
The second line with n integers, expressed sequence A;
The third line with m integers, expressed sequence B;

输出
For each set of test cases, output the length of the longest common subsequence of A and B, in a single line.

样例输入
5 4
1 2 6 5 4
1 3 5 4

样例输出
3

上传者
TC_胡仁东

解题:一种LCS转LCS的nlogn的算法。是严格上升的LCS。

首先是LCS,我们把a序列中的每个元素在b中出现的位置保存起来,再按照降序排列,排列后再代入a的每个对应元素,那就转化为了求这个新的序列的最长上升子序列了。如:a[] = {a, b, c,} b[] = {a,b,c,b,a,d},那么a中的a,b,c在b中出现的位置分别就是{0,4},{1,3},{2}。分别按降序排列后代入a序列就是{4,0,2,3,1},之所以要按照降序排列,目的就是为了让每个元素只取到一次。

接下来的问题就是要求最长升序子序列问题了,也就是求LIS。

特殊情况下,会退化得很严重。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <climits>
  7. #include <vector>
  8. #include <queue>
  9. #include <cstdlib>
  10. #include <string>
  11. #include <set>
  12. #include <stack>
  13. #define LL long long
  14. #define pii pair<int,int>
  15. #define INF 0x3f3f3f3f
  16. using namespace std;
  17. struct info{
  18. int num,pos;
  19. };
  20. int n,m,tot,sa[],sc[],q[],head,tail;
  21. info sb[];
  22. bool cmp(const info &x,const info &y){
  23. return x.num < y.num;
  24. }
  25. int bsearch(int lt,int rt,int val){
  26. int mid,pos = -;
  27. while(lt <= rt){
  28. int mid = (lt+rt)>>;
  29. if(val <= sb[mid].num){
  30. pos = mid;
  31. rt = mid-;
  32. }else lt = mid+;
  33. }
  34. return pos;
  35. }
  36. int binsearch(int lt,int rt,int val){
  37. while(lt <= rt){
  38. int mid = (lt+rt)>>;
  39. if(q[mid] < val) lt = mid+;
  40. else rt = mid-;
  41. }
  42. return lt;
  43. }
  44. int main() {
  45. while(~scanf("%d %d",&n,&m)){
  46. head = tail = tot = ;
  47. for(int i = ; i <= n; i++) scanf("%d",sa+i);
  48. for(int i = ; i <= m; i++){
  49. scanf("%d",&sb[i].num);
  50. sb[i].pos = i;
  51. }
  52. sort(sb+,sb+m+,cmp);
  53. for(int i = ; i <= n; i++){
  54. int tmp = bsearch(,m,sa[i]);
  55. while(tmp > && sb[tmp].num == sa[i]) sc[tot++] = sb[tmp++].pos;
  56. }
  57. for(int i = ; i < tot; i++){
  58. if(head == tail || q[head-] < sc[i]){
  59. q[head++] = sc[i];
  60. }else{
  61. int tmp = binsearch(tail,head-,sc[i]);
  62. q[tmp] = sc[i];
  63. }
  64. }
  65. printf("%d\n",head-tail);
  66. }
  67. return ;
  68. }

NYIST 760 See LCS again的更多相关文章

  1. nyoj 760 See LCS again

    See LCS again 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 There are A, B two sequences, the number of ele ...

  2. NYOJ 36 LCS(最长公共子序列)

    题目链接: http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=36 最长公共子序列 时间限制:3000 ms  |  内存限制:65535 KB ...

  3. nyoj36-最长公共子序列 (LCS)

    http://acm.nyist.net/JudgeOnline/problem.php?pid=36 最长公共子序列 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 ...

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

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

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

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

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

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

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

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

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

  9. 删除部分字符使其变成回文串问题——最长公共子序列(LCS)问题

    先要搞明白:最长公共子串和最长公共子序列的区别.    最长公共子串(Longest Common Substirng):连续 最长公共子序列(Longest Common Subsequence,L ...

随机推荐

  1. luogu 3808 【模板】AC自动机(简单版)

    我太菜了 棒神%%% #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib ...

  2. [Swift通天遁地]八、媒体与动画-(4)给相机添加CoreImage滤镜效果

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  3. @RequestParam 和 @RequestBody 接受的时间格式

    这两个接受的时间格式不相同 首先看一下他们的区别 @RequestParam用来处理Content-Type: 为 application/x-www-form-urlencoded编码的内容.(Ht ...

  4. ACM算法目录

    数据结构 栈,队列,链表 •哈希表,哈希数组 •堆,优先队列 双端队列 可并堆 左偏堆 •二叉查找树 Treap 伸展树 •并查集 集合计数问题 二分图的识别 •平衡二叉树 •二叉排序树 •线段树 一 ...

  5. [Luogu 2678] noip15 子串

    [Luogu 2678] noip15 子串 题目描述 有两个仅包含小写英文字母的字符串 A 和 B.现在要从字符串 A 中取出 k 个互不重叠的非空子串,然后把这 k 个子串按照其在字符串 A 中出 ...

  6. cocos2d-js 添加广告

    http://www.cocoachina.com/bbs/read.php?tid=225655

  7. js常用操作~~~~将持续更新

    1.替换多个模板变量 var s="my javascript is very poor,who can help me?" var reg=/(\w*)my(.*)is(.*)c ...

  8. TensorFlow学习---入门(一)-----MNIST机器学习

    参考教程:http://www.tensorfly.cn/tfdoc/tutorials/mnist_beginners.html 数据下载地址:http://wiki.jikexueyuan.com ...

  9. node 第三方包学习

    时间格式化 moment var moment = require('moment'); moment().format();

  10. CSS——轮播图中的箭头

    注意事项: 1.定位中left权重比right高,top权重比bottom高 2.两个span标签嵌套在一个盒子中,将来显示隐藏只需要控制父盒子就行了 <!DOCTYPE html> &l ...