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

题意:同样数字能够连接可是必须和不同数字的连接交叉。问最大可能性

dp[i][j]表示第一行的前i个和第二行的前j个的最大可能。

#include<limits.h>
using namespace std;
int a[110],b[110];
int dp[110][110];
int n,m,t; int main()
{
int k1,k2;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int j=1;j<=m;j++)
scanf("%d",&b[j]);
memset(dp,0,sizeof(dp));
for(int i=2;i<=n;i++)
{
for(int j=2;j<=m;j++)
{
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);//相等时可达到的dp[i][j]的状态的最大值
if(a[i]!=b[j])
{
for(k1=i;k1>=1;k1--)
{
if(b[j]==a[k1])
break;
}
for(k2=j;k2>=1;k2--)
{
if(a[i]==b[k2])
break;
}
if(k1&&k2)
dp[i][j]=max(dp[i][j],dp[k1-1][k2-1]+2);//更新dp[i][j]
}
}
}
printf("%d\n",dp[n][m]);
}
return 0;
}

POJ 1692 Crossed Matchings(DP)的更多相关文章

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

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

  2. 【POJ 3071】 Football(DP)

    [POJ 3071] Football(DP) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4350   Accepted ...

  3. 【POJ】1692 Crossed Matchings

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

  4. POJ 1260:Pearls(DP)

    http://poj.org/problem?id=1260 Pearls Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8 ...

  5. POJ 2192 :Zipper(DP)

    http://poj.org/problem?id=2192 Zipper Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1 ...

  6. POJ 3254 Corn Fields(DP + 状态压缩)

    题目链接:http://poj.org/problem?id=3254 题目大意:Farmer John 放牧cow,有些草地上的草是不能吃的,用0表示,然后规定两头牛不能相邻放牧.问你有多少种放牧方 ...

  7. poj - 1191 - 棋盘切割(dp)

    题意:将一个8*8的棋盘(每一个单元正方形有个分值)沿直线(竖或横)割掉一块,留下一块,对留下的这块继续这样操作,总共进行n - 1次,得到n块(1 < n < 15)矩形,每一个矩形的分 ...

  8. POJ 1160 Post Office(DP+经典预处理)

    题目链接:http://poj.org/problem?id=1160 题目大意:在v个村庄中建立p个邮局,求所有村庄到它最近的邮局的距离和,村庄在一条直线上,邮局建在村庄上. 解题思路:设dp[i] ...

  9. POJ 1191 棋盘分割(DP)

    题目链接 题意 : 中文题不详述. 思路 : 黑书上116页讲的很详细.不过你需要在之前预处理一下面积,那样的话之后列式子比较方便一些. 先把均方差那个公式变形, 另X表示x的平均值,两边平方得 平均 ...

随机推荐

  1. git 使用笔记(三)-分支的使用

    简单介绍 之前说过,每次修改之后,Git 并不是保存这些修改之后的差异变化,实际上就像一个照相机一样,将修改后的文件拍下作为文件快照,记录在一个微型的文件系统中.在 Git 中提交时,会保存一个提交对 ...

  2. SQLite For .Net 已经整合了32位和64位

    以前引用SQLite.DLL的时候,如果是winform等桌面程序,还要分32位和64位不一样的DLL,但最近已经整合为一个包了 打开vs的程序包管理器控制器,输入: install-package ...

  3. 获取多个div,点击第几个,显示第几个

    1.闭包:函数内部又定义了一个函数,内部函数引用外部函数的变量,就构成了闭包. <script type="text/javascript"> var divs = d ...

  4. 写后台SQL的一些心得

    昨天犯了一个错,其实是前几天写的代码犯的错,今天发现的.这是原来的代码: <update id="updateInfoByFoodId"> update food se ...

  5. Spring—Hibernate

    1.家jar包 2配置applicationContext与xxx.hbm.xml(根据需要决定是否配置hibernate.hbm.xml) applicationContext.xml <?x ...

  6. 关于LZO和LZOP

    LZO  是一个适合实时解压.压缩的压缩库 LZOP 基于LZO库的压缩解压工具   PS:有了压缩解压库LZO,还不能直接操作文件压缩解压,需要LZOP   下载的话直接google吧~~~  

  7. 8.PHP 教程_PHP字符串

    字符串变量用于存储并处理文本. PHP中的字符串变量 字符串变量用于包含有字符的值. 在创建字符串之后,我们就可以对它进行操作了.您可以直接在函数中使用字符串,或者把它存储在变量中. 在下面的实例中, ...

  8. C#中小函数的应用

    今天看到的一段函数 StringBuilder sb = new StringBuilder("sselect * from table where 1=1"); if(TextB ...

  9. IOS 使用程序外地图(IOS Map and google Map)

    1.调用IOS6苹果地图 IOS6中实现这个功能需要使用Map Kit中的MKPlaceMark和MKMapItem两个类,因此我们需要在工程中添加MapKit.framework主要代码如下: - ...

  10. TexturePacker

    TexturePacker 可以免费申请,希望可以申请到.