zoukankan      html  css  js  c++  java
  • nyoj 760 See LCS again

    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
     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<algorithm>
     4 #include<iostream>
     5 using namespace std;
     6 int dp[100005],g[100005];
     7 int main()
     8 {
     9     int n,m;
    10     while(~scanf("%d%d",&n,&m))
    11     {
    12         memset(dp,0,sizeof(dp));
    13         int x;
    14         for(int i = 1; i <= n ; ++ i)
    15             scanf("%d",&x),dp[x]=i;
    16         int r = 0 ;
    17         for(int i = 1 ; i <= m ; ++ i)
    18         {
    19             scanf("%d",&x);
    20             if(dp[x])
    21             g[r++]=dp[x];
    22         }
    23         int p = 0 ;
    24         dp[p++] = g[0];
    25         for(int i = 1 ; i < r ; ++ i)
    26         if(dp[p-1] < g[i])
    27             dp[p++] = g[i];
    28         else
    29         {
    30             x  = lower_bound(dp,dp+p,g[i])-dp;
    31             dp[x] = g[i];
    32         }
    33         printf("%d
    ",p);
    34     }
    35     return 0;
    36 }

    函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回大于或等于val的第一个元素位置。如果所有元素都小于val,则返回last的位置,且last的位置是越界的!
    返回查找元素的第一个可安插位置,也就是“元素值>=查找值”的第一个元素的位置

  • 相关阅读:
    recyclerView DiffUtil使用
    RecyclerView实现侧滑删除、置顶、滑动
    Android RecyclerView
    视频框架 Vitamio使用
    react native初始化项目
    react native环境搭建
    javascript Promise
    javascript 基础
    [IOS学习笔记]KVO
    [IOS学习笔记] UINavigationController Demo
  • 原文地址:https://www.cnblogs.com/lovychen/p/3650170.html
Copyright © 2011-2022 走看看