Time Limit: 1000MS

    Memory Limit: 10000K
Total Submissions: 2738   Accepted: 1777

Description

There are two rows of positive integer numbers. We can draw one line segment between any two equal numbers, with values r, if one of them is located in the first row and the other one is located in the second row. We call this line segment an r-matching segment. The following figure shows a 3-matching and a 2-matching segment.


We want to find the maximum number of matching segments possible to draw for the given input, such that:

1. Each a-matching segment should cross exactly one b-matching segment, where a != b .

2. No two matching segments can be drawn from a number. For example, the following matchings are not allowed.



Write a program to compute the maximum number of matching segments for the input data. Note that this number is always even.

Input

The
first line of the input is the number M, which is the number of test
cases (1 <= M <= 10). Each test case has three lines. The first
line contains N1 and N2, the number of integers on the first and the
second row respectively. The next line contains N1 integers which are
the numbers on the first row. The third line contains N2 integers which
are the numbers on the second row. All numbers are positive integers
less than 100.

Output

Output
should have one separate line for each test case. The maximum number of
matching segments for each test case should be written in one separate
line.

Sample Input

3
6 6
1 3 1 3 1 3
3 1 3 1 3 1
4 4
1 1 3 3
1 1 3 3
12 11
1 2 3 3 2 4 1 5 1 3 5 10
3 1 2 3 2 4 12 1 5 5 3

Sample Output

6
0
8

Source

两个交叉的匹配为一组,每找到一组可行的匹配,答案数+2 。

设:f[上方匹配位置][下方匹配位置]=最优解

假设现在扫到了上方数组的i点和下方数组的j点。首先可以想到如果没有新的匹配,f[i][j]=max(f[i][j-1],f[i-1][j])

接着考虑新的匹配,在上方数组中从i往前找,找到最近的pos1使a[pos1]=b[j],同理在下方找到b[pos2]=a[i],那么pos1-j,pos2-i两条连线必然交叉,得到动归方程:

f[i][j]=max(f[i][j],f[pos1-1][pos2-1]+2)

 /**/
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int mxn=;
int n1,n2;
int a[mxn],b[mxn];
int f[mxn][mxn];
int main(){
int T;
scanf("%d",&T);
int i,j;
while(T--){
memset(f,,sizeof f);
scanf("%d%d",&n1,&n2);
for(i=;i<=n1;i++)scanf("%d",&a[i]);
for(i=;i<=n2;i++)scanf("%d",&b[i]);
for(i=;i<=n1;i++)
for(j=;j<=n2;j++){
f[i][j]=max(f[i][j-],f[i-][j]);
if(a[i]==b[j])continue;
int k=i-;
while(k && a[k]!=b[j])k--;int pos1=k;
k=j-;
while(k && b[k]!=a[i])k--;int pos2=k;
if(pos1&&pos2) f[i][j]=max(f[i][j],f[pos1-][pos2-]+);
}
printf("%d\n",f[n1][n2]);
}
return ;
}

最新文章

  1. Shader_2[杂]
  2. 【mysql】Blob类型
  3. webservice的简单示例的实现步骤
  4. NET中MSMQ的使用----附例子
  5. 用Chrome浏览器模拟手机,android,iphone,ipad访问网站
  6. 小生经验贴 --- adapter的数据更新
  7. bzoj3611
  8. c#中serialPort1_DataReceived串口接收事件处理
  9. (hdu step 7.1.2)You can Solve a Geometry Problem too(乞讨n条线段,相交两者之间的段数)
  10. 10970 - Big Chocolate
  11. Linux(CentOS6.5)下创建新用户和组,并制定用户和组ID
  12. dataview findrows
  13. sublime text 3配置c/c++编译环境
  14. leetcode第27题:移除指定元素
  15. 一个全栈Web/移动App开发学习路径
  16. [转帖]Windows 内置端口转发功能
  17. SQL数据缓存依赖总结
  18. 微服务深入浅出(6)-- 熔断器Hystrix
  19. PLSQL 触发器
  20. PAT 1016 部分A+B C语言

热门文章

  1. Django 模型与 Mysql 数据类型对应
  2. PHP PDO 使用类
  3. 如何在 Linux 中配置基于密钥认证的 SSH
  4. 四 python并发编程之协程
  5. linux下mysql的权限设计总结
  6. beautifulsoup解析
  7. centos6.9系统安装
  8. windows下pip安装python模块时报错【转】
  9. main方法中sleep
  10. 「微信小程序免费辅导教程」25,基本内容组件text的使用及个人帐号允许的服务类目