Crossed Matchings


Time Limit: 2 Seconds      Memory Limit: 65536 KB

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 file 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 file 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

 /*
zoj 1425 最大交叉匹配
题目大意:给两行序列,求他们的最大交叉数*2。
定义交叉:上下相同的数字连线,两条这样的线相交,
每个数字只能用与一条线,且两条线的数字不等。
定义dp(i,j)表示第一行至i,第二行至j,的最大交叉数,
dp(i,j)=max(dp(i-1,j-1)((i,j都不是))dp(i-1,j)(i不是),dp(i,j-1)(j不是),dp(n-1,m-1)+1(n-j,m-i交叉))。
*/
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; const int maxn=;
int dp[maxn][maxn];
int a[maxn],b[maxn]; inline int max(int a,int b){return a>b?a:b;} int main()
{
int t,i,j,k,l,n,m;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(i=;i<=n;i++) scanf("%d",a+i);
for(i=;i<=m;i++) scanf("%d",b+i);
memset(dp,,sizeof(dp));
for(i=;i<=n;i++)
{
for(j=;j<=m;j++)
{
dp[i][j]=max(dp[i-][j-],max(dp[i-][j],dp[i][j-]));
if(a[i]==b[j]) continue;//两条交叉线段必须数字不同
k=i-;l=j-;
while(k> && a[k]!=b[j]) k--;
while(l> && a[i]!=b[l]) l--;
if(k && l) dp[i][j]=max(dp[k-][l-]+,dp[i][j]);
}
}
printf("%d\n",dp[n][m]*);
}
return ;
}

最新文章

  1. Entity Framework Code First实体关联数据加载
  2. js实现多张图片每隔一秒换一张图片
  3. 记第一次TopCoder, 练习SRM 583 div2 250
  4. Ubuntu 使用Cisco VPN、AnyConnect、OpenConnect的方法。
  5. JavaScript是如何实现继承的(六种方式)
  6. 第一零二天上课 PHP TP框架 引入文件路径问题和调用验证码的方式
  7. java笔记--关于int和byte[]的转换
  8. Spring.Scheduling.Quartz的使用
  9. Spring的事务传播属性,数据库的隔离级别
  10. MongoDB自定义函数部分 定义及引用
  11. java.sql.SQLException: Can not issue executeUpdate() for SELECTs
  12. TortoiseSVN 1.8 关于右键的设置
  13. 如何使用Git上传代码到GitHub
  14. 【Python的迭代器,生成器】
  15. watchdog(IWDG)
  16. 玩玩微信公众号Java版之三:access_token及存储access_token
  17. &lt;EffectiveJava&gt;读书笔记--02泛型数组
  18. [NOI 2011]道路修建
  19. 微信小程序(基本知识点)
  20. centos搭建git服务

热门文章

  1. Feign-手动创建FeignClient
  2. Luogu P1666 前缀单词
  3. Oracle数据库常用的Sql语句整理
  4. iOS动画之iOS UIBezierPath类 介绍
  5. JDBC-防止SQL注入问题
  6. 03Qt信号与槽(2)
  7. 老男孩Python高级全栈开发工程师三期完整无加密带课件(共104天)
  8. destoon 信息发布表单提交验证
  9. laravel连接数据库提示mysql_connect() :Connection refused...
  10. python3 发邮件 smtplib &amp; email 库