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

POJ1692 Crossed Matchings的更多相关文章

  1. [ACM_动态规划] ZOJ 1425 Crossed Matchings(交叉最大匹配 动态规划)

    Description There are two rows of positive integer numbers. We can draw one line segment between any ...

  2. POJ 1692 Crossed Matchings(DP)

    Description There are two rows of positive integer numbers. We can draw one line segment between any ...

  3. 【POJ】1692 Crossed Matchings

    经典DP,想了很久,开始想复杂了. #include <iostream> using namespace std; #define MAXNUM 100 int mymax(int a, ...

  4. POJ 1692 Crossed Matchings dp[][] 比较有意思的dp

    http://poj.org/problem?id=1692 这题看完题后就觉得我肯定不会的了,但是题解却很好理解.- - ,做题阴影吗 所以我还是需要多思考. 题目是给定两个数组,要求找出最大匹配数 ...

  5. 别人整理的DP大全(转)

    动态规划 动态规划 容易: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ...

  6. dp题目列表

    此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 11 ...

  7. poj 动态规划题目列表及总结

    此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 11 ...

  8. [转] POJ DP问题

    列表一:经典题目题号:容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1191,1208, 1276, 13 ...

  9. poj动态规划列表

    [1]POJ 动态规划题目列表 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 13 ...

随机推荐

  1. React报错 :browserHistory doesn't exist in react-router

    由于版本问题,React中history不可用 import { hashHistory } from 'react-router' 首先应该导入react-router-dom包: import { ...

  2. Linux MySQL 8.0 忘记密码

    不小忘了MySQL的密码,按照书上和网上的内容都没能修改成功,终于在借鉴了多篇文章成功之后找到原因,修改密码成功 修改 MySQL 密码 第一步:关闭 MySQL 进程 systemctl stop ...

  3. 零基础快速掌握Python系统管理视频课程【猎豹网校】

    点击了解更多Python课程>>> 零基础快速掌握Python系统管理视频课程[猎豹网校] 课程目录 01.第01章 Python简介.mp4 02.第02章 IPython基础.m ...

  4. thinkphp-PHP实现Excel导入 导出功能

    Excel导出 //功能:导出题库模板 public function get_contract_ex() { ob_get_clean(); header("Content-Typ:tex ...

  5. 37.VUE学习之-表单的综合运用

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  6. python3爬虫之Urllib库(一)

    上一篇我简单说了说爬虫的原理,这一篇我们来讲讲python自带的请求库:urllib 在python2里边,用urllib库和urllib2库来实现请求的发送,但是在python3种在也不用那么麻烦了 ...

  7. [Hdu3555] Bomb(数位DP)

    Description 题意就是找0到N有多少个数中含有49. \(1\leq N \leq2^{63}-1\) Solution 数位DP,与hdu3652类似 \(F[i][state]\)表示位 ...

  8. HDU_6194 后缀数组+RMQ

    好绝望的..想了五个多小时,最后还是没A...赛后看了下后缀数组瞬间就有了思路...不过因为太菜,想了将近两个小时才吧这个题干掉. 首先,应当认为,后缀数组的定义是,某字符串S的所有后缀按照字典序有小 ...

  9. P2615 神奇的幻方

    P2615 神奇的幻方 题目描述 幻方是一种很神奇的N*N矩阵:它由数字1,2,3,……,N*N构成,且每行.每列及两条对角线上的数字之和都相同. 当N为奇数时,我们可以通过以下方法构建一个幻方: 首 ...

  10. Failed to find provider info for com.tencent.mm.sdk.plugin.provider

    微信启动的时候可以调用,微信没启动,调用支付报这个错误. 我的问题是 微信发开着的jar包不是最新的,去官方网站下一个最新的就可以成功了.