nyoj 760 See LCS again
See LCS again
- 描述
-
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
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
int dp[],g[];
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
memset(dp,,sizeof(dp));
int x;
for(int i = ; i <= n ; ++ i)
scanf("%d",&x),dp[x]=i;
int r = ;
for(int i = ; i <= m ; ++ i)
{
scanf("%d",&x);
if(dp[x])
g[r++]=dp[x];
}
int p = ;
dp[p++] = g[];
for(int i = ; i < r ; ++ i)
if(dp[p-] < g[i])
dp[p++] = g[i];
else
{
x = lower_bound(dp,dp+p,g[i])-dp;
dp[x] = g[i];
}
printf("%d\n",p);
}
return ;
}函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回大于或等于val的第一个元素位置。如果所有元素都小于val,则返回last的位置,且last的位置是越界的!
返回查找元素的第一个可安插位置,也就是“元素值>=查找值”的第一个元素的位置
nyoj 760 See LCS again的更多相关文章
- NYIST 760 See LCS again
See LCS again时间限制:1000 ms | 内存限制:65535 KB难度:3 描述There are A, B two sequences, the number of elements ...
- nyoj 36 最长公共子序列【LCS模板】
最长公共子序列 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 咱们就不拐弯抹角了,如题,需要你做的就是写一个程序,得出最长公共子序列.tip:最长公共子序列也称作最 ...
- NYOJ 36 LCS(最长公共子序列)
题目链接: http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=36 最长公共子序列 时间限制:3000 ms | 内存限制:65535 KB ...
- nyoj 37-回文字符串(reverse, 动态规划, lcs)
37-回文字符串 内存限制:64MB 时间限制:3000ms Special Judge: No accepted:10 submit:17 题目描述: 所谓回文字符串,就是一个字符串,从左到右读和从 ...
- nyoj 36-最长公共子序列 (动态规划,DP, LCS)
36-最长公共子序列 内存限制:64MB 时间限制:3000ms Special Judge: No accepted:18 submit:38 题目描述: 咱们就不拐弯抹角了,如题,需要你做的就是写 ...
- NYOJ 1007
在博客NYOJ 998 中已经写过计算欧拉函数的三种方法,这里不再赘述. 本题也是对欧拉函数的应用的考查,不过考查了另外一个数论基本定理:如何用欧拉函数求小于n且与n互质所有的正整数的和. 记eule ...
- NYOJ 998
这道题是欧拉函数的使用,这里简要介绍下欧拉函数. 欧拉函数定义为:对于正整数n,欧拉函数是指不超过n且与n互质的正整数的个数. 欧拉函数的性质:1.设n = p1a1p2a2p3a3p4a4...pk ...
- 我的第一篇博客----LCS学习笔记
LCS引论 在这篇博文中,博主要给大家讲一个算法----最长公共子序列(LCS)算法.我最初接触这个算法是在高中学信息学竞赛的时候.那时候花了好长时间理解这个算法.老师经常说,这种算法是母算法,即从这 ...
- 动态规划之最长公共子序列(LCS)
转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...
随机推荐
- OpenCV Harris 角点检测子
Harris 角点检测子 目标 本教程中我们将涉及: 有哪些特征?它们有什么用? 使用函数 cornerHarris 通过 Harris-Stephens方法检测角点. 理论 有哪些特征? 在计算机视 ...
- ContextLoaderListener 与 ServletDispatcher
网上找了一下关于ContextLoaderListener和ServletDispatcher的解释,这是原文 http://simone-folino.blogspot.com/2012/05/di ...
- qsort(),sort()排序函数
一.qsort()函数 功 能: 使用快速排序例程进行排序 头文件:stdlib.h 用 法: void qsort(void *base,int nelem,int width,int (*fcmp ...
- JavaScript的学习要点
概要 了解Javascript历史以及Javascript三个不同组成部分: ECMAScript DOM(文档对象模型) BOM(浏览器对象模型) ECMAScript 目标 掌握Javascrip ...
- SQL中truncate table和delete的区别
truncate table table_name delete from table_name drop table table_name truncate table在功能上与不带where子句的 ...
- javascript实现浏览器窗口传递参数
a.html <html> <head> <title>主页面</title> <script language="javascript ...
- SQL insert失败时也会造成自增长字段加1
CREATE TABLE #test(id INT IDENTITY(1,1), NAME varchar(30)) INSERT #test(name)SELECT '41545' SELECT ...
- Packagist / Composer 中国全量镜像
用法: 有两种方式启用本镜像服务: 将配置信息添加到 Composer 的配置文件 config.json 中(系统全局配置).见“例1 (推荐方式)” 将配置信息添加到单个项目的 composer. ...
- dede 怎样调用其它栏目的文章或者缩略图列表且有分页效果?
提问i:我做一个站点.有5个栏目,第5个栏目想把前4个栏目的文章都调用一下,搞一个汇总的文章集合. 发现用arclist调用不能设置pagesize的属性.从而不能给文章分页.然而list貌似不能调用 ...
- [Big Data] Week 5 A (Advance)
Question 1 Consider the diagonal matrix M = 1 0 0 0 2 0 0 0 0 . Compute its Moore-Penrose pseudoinve ...