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 ;
}
 

最新文章

  1. W3School-CSS 表格实例
  2. 常见数据结构之JavaScript实现
  3. 为什么C#不使用多继承?(from stackoverflow)
  4. UITextFielddelegate委托方法注释
  5. UILabel自适应高、宽
  6. Net中的反射使用入门
  7. LightOj_1104 Birthday Paradox
  8. [目录][总结] C++和Java 中的主要操作对比
  9. 2013年全球ERP市场格局(Gartner)
  10. VB2012读取xml
  11. swift优秀学习博客
  12. mysql数据库主从备份
  13. 配置trunk
  14. [linux] VirtualBox复制虚拟机
  15. jdk9+版本的bug
  16. TextBox显示提示信息
  17. Duilib嵌入CEF出现窗口显示不正常
  18. day 020 常用模块02
  19. C# 模拟多线程下载文件
  20. Netty权威指南之NIO通信模型

热门文章

  1. 123. Best Time to Buy and Sell Stock III (Array; DP)
  2. lambda表达式&amp;map&amp;filter&amp;yield
  3. python的select服务端的代码和客户端的代码
  4. python之socket运用1
  5. 硬盘的 read0 read 1
  6. swift VFL - 父视图是scrollview 注意点
  7. iOS - OC - XML 解析 - NSXMLParser
  8. 14- Servlet.service() for servlet [mvc-dispatcher] in context with path [/collegeservice] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root caus
  9. Luogu 2912 [USACO08OCT]牧场散步Pasture Walking
  10. json.dumps错误:&#39;utf8&#39; codec can&#39;t decode byte解决方案-乾颐堂