Virus 

We have a log file, which is a sequence of recorded events. Naturally, the timestamps are strictly increasing.

However, it is infected by a virus, so random records are inserted (but the order of original events is preserved). The backup log file is also infected, but since the virus is making changes randomly, the two logs are now different.

Given the two infected logs, your task is to find the longest possible original log file. Note that there might be duplicated timestamps in an infected log, but the original log file will not have duplicated timestamps.

Input

The first line contains T (   T100), the number of test cases. Each of the following lines contains two lines, describing the two logs in the same format. Each log starts with an integer n (   1n1000), the number of events in the log, which is followed by n positive integers not greater than 100,000, the timestamps of the events, in the same order as they appear in the log.

Output

For each test case, print the number of events in the longest possible original log file.

Sample Input

1
9 1 4 2 6 3 8 5 9 1
6 2 7 6 3 5 1

Sample Output

3

题目大意:T种情况,每种情况2行数据,每行数据第一个表示个数,接下来是一个序列,问两组数据的最长公共递增子序列的长度。
解题思路:当看到这题想到的是LCS和LIS问题,没错这题也是动态规划问题,只要找到状态转移方程就可轻易搞定!
     >_<:LIS设DP[i]表示以第i个数字结尾的最长上升子序列的长度
     >0<:DP[i]=max(DP[j]+1){1<=j<=i-1}
  
   >_<:LCS设DP[i][j]表示以A串第i个字符结尾以B串第j个字符结尾的最长字串
     >0<:当a[i]==b[j]时:DP[i][j]=DP{i-1][j-1]+1;
       当a[i]!=b[j]时:DP[i][j]=max(DP[i-1][j],DP[i][j-1])
     >_<:LCIS设F[i][j]表示以a串前i个字符b串的前j个字符且以b[j]为结尾构成的LCIS的长度
     >0<:当a[i]!=b[j]时:F[i][j]=F[i-1][j]
       当a[i]==b[j]时:F[i][j]=max(F[i-1][k])+1 1<=k<=j-1 && b[j]>b[k]
 #include<iostream>
#include<cstdio>
#include<string>
#include<string.h>
#include<cstring>
#include<cmath>
#include<sstream>
#include<iomanip>
#include<algorithm>
#include<vector>
using namespace std;
int f[][],a[],b[],i,j,t,n1,n2,maxn;
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d",&n1);
for(i=;i<=n1;i++) scanf("%d",&a[i]);
scanf("%d",&n2);
for(i=;i<=n2;i++) scanf("%d",&b[i]);
memset(f,,sizeof(f));
for(i=;i<=n1;i++)
{
maxn=;
for(j=;j<=n2;j++)
{
f[i][j]=f[i-][j];//不相等
if (a[i]>b[j]&&maxn<f[i-][j]) maxn=f[i-][j];//更新maxn
if (a[i]==b[j]) f[i][j]=maxn+;//相等
}
}
maxn=;
for(i=;i<=n2;i++)if(maxn<f[n1][i])maxn=f[n1][i];
printf("%d\n",maxn);
}
return ;
}
 

[ACM_动态规划] UVA 12511 Virus [最长公共递增子序列 LCIS 动态规划]的更多相关文章

  1. hdu 1423 最长公共递增子序列 LCIS

    最长公共上升子序列(LCIS)的O(n^2)算法 预备知识:动态规划的基本思想,LCS,LIS. 问题:字符串a,字符串b,求a和b的LCIS(最长公共上升子序列). 首先我们可以看到,这个问题具有相 ...

  2. UVA 12511/CSU 1120 virus 最长公共上升子序列

    第一次接触一个这最长公共上升子序列 不过其实搞清楚了跟最长公共子序列和 最长上升子序列如出一辙 两重循环,对于当前不相等的,等于前一个的值,相等的,等于比当前A[i]小的最大值+1.弄个临时变量记录最 ...

  3. 动态规划——最长公共上升子序列LCIS

    问题 给定两个序列A和B,序列的子序列是指按照索引逐渐增加的顺序,从原序列中取出若干个数形成的一个子集,若子序列的数值大小是逐渐递增的则为上升子序列,若A和B取出的两个子序列A1和B1是相同的,则A1 ...

  4. 最长公共上升子序列(LCIS)

    最长公共上升子序列慕名而知是两个字符串a,b的最长公共递增序列,不一定非得是连续的.刚开始看到的时候想的是先用求最长公共子序列,然后再从其中找到最长递增子序列,可是仔细想一想觉得这样有点不妥,然后从网 ...

  5. hdu 1423 最长公共递增子序列

    这题一开始把我给坑了,我还没知道LCIS的算法,然后就慢慢搞吧,幸运的是还真写出来了,只不过麻烦了一点. 我是将该题转换为多条线段相交,然后找出最多多少条不相交,并且其数值死递增的. 代码如下: #i ...

  6. HDU1423 最长公共上升子序列LCIS

    Problem Description This is a problem from ZOJ 2432.To make it easyer,you just need output the lengt ...

  7. HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】

    HDOJ 1423 Greatest Common Increasing Subsequence [DP][最长公共上升子序列] Time Limit: 2000/1000 MS (Java/Othe ...

  8. LCIS最长公共上升子序列

    最长公共上升子序列LCIS,如字面意思,就是在对于两个数列A和B的最长的单调递增的公共子序列. 这道题目是LCS和LIS的综合. 在LIS中,我们通过两重循环枚举当序列以当前位置为结尾时,A序列中当前 ...

  9. 动态规划(一)——最长公共子序列和最长公共子串

    注: 最长公共子序列采用动态规划解决,由于子问题重叠,故采用数组缓存结果,保存最佳取值方向.输出结果时,则自顶向下建立二叉树,自底向上输出,则这过程中没有分叉路,结果唯一. 最长公共子串采用参考串方式 ...

随机推荐

  1. Web标准:二、一列布局

    知识点: 1.一列固定宽度 2.一列固定宽度居中 3.一列自适应宽度 4.一列自适应宽度居中 5.一列二至多块布局   1)一列固定宽度 下图是定义了一个高300px,宽400px,颜色是#99FFc ...

  2. Neuron network

    关于神经网络你不能不知道的一切 作者|Kailash Ahirwar 编译|Sambodhi 编辑|Vincent AI前线导语:理解什么是人工智能,以及机器学习和深度学习是如何影响人工智能的,这是一 ...

  3. php.ini memory_limit引起的问题

    故障现象    在运行PHP程序,通常会遇到“Fatal Error: Allowed memory size of xxxxxx bytes exhausted”的错误, 这个意味着PHP脚本使用了 ...

  4. js string 字符串

    mutil lines string 多行字符串, 由于多行字符串用\n写起来比较费事,所以最新的ES6标准新增了一种多行字符串的表示方法,用...表示,是单撇号, 不是单引号. 这是一个 多行 字符 ...

  5. linux 下 php 安装 event

    1.下载event源码包 https://pecl.php.net/package/event 如:event-2.0.4.tgz 2.解压 > tar zxvf event-2.0.4.tgz ...

  6. Linux多线程服务端编程 使用muduo C++网络库 学习笔记 日志log

    代码来自陈硕开源代码库 muduo中 地址是https://github.com/chenshuo/muduo #pragma once #include <string> #define ...

  7. Jquery Mobile 随记

    1. 设置全局的页面过渡效果 $.mobile.defaultDialogTransition='none';

  8. cocos2d接安卓facebook插件(已测cocos-x 3.7 3.8版本)

    1  控制台创建新工程: a 控制台 进入cocos2d文件夹下面,如cocos2d-x-3.7.1,执行setup.py,未设置NDK SDK ANT 路径的设置路径,需要改路径的 explore ...

  9. 移动文件读/写指针----lseek

    头文件:#include<sys/types.h>.#include<unistd.h> 函数原型:off_t lseek(int fildes,off_t offset,in ...

  10. <asp:RadioButton> 选项判断

    小菜在项目中遇到一个<asp:RadioButton> 选择之后,让其控制其他标签显示或者隐藏的问题.记录以备忘记之需! <html xmlns="http://www.w ...