码迷,mamicode.com
首页 > 其他好文 > 详细

zoj 1425 最大交叉匹配

时间:2014-11-11 18:49:49      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   ar   os   sp   for   

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.

bubuko.com,布布扣

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.

bubuko.com,布布扣

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

 1 /*
 2 zoj 1425 最大交叉匹配
 3 题目大意:给两行序列,求他们的最大交叉数*2。
 4 定义交叉:上下相同的数字连线,两条这样的线相交,
 5 每个数字只能用与一条线,且两条线的数字不等。
 6 定义dp(i,j)表示第一行至i,第二行至j,的最大交叉数,
 7 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交叉))。
 8 */
 9 #include <iostream>
10 #include <cstdio>
11 #include <cstring>
12 using namespace std;
13 
14 const int maxn=105;
15 int dp[maxn][maxn];
16 int a[maxn],b[maxn];
17 
18 inline int max(int a,int b){return a>b?a:b;}
19 
20 int main()
21 {
22     int t,i,j,k,l,n,m;
23     scanf("%d",&t);
24     while(t--)
25     {
26         scanf("%d%d",&n,&m);
27         for(i=1;i<=n;i++) scanf("%d",a+i);
28         for(i=1;i<=m;i++) scanf("%d",b+i);
29         memset(dp,0,sizeof(dp));
30         for(i=1;i<=n;i++)
31         {
32             for(j=1;j<=m;j++)
33             {
34                 dp[i][j]=max(dp[i-1][j-1],max(dp[i-1][j],dp[i][j-1]));
35                 if(a[i]==b[j]) continue;//两条交叉线段必须数字不同
36                 k=i-1;l=j-1;
37                 while(k>0 && a[k]!=b[j]) k--;
38                 while(l>0 && a[i]!=b[l]) l--;
39                 if(k && l) dp[i][j]=max(dp[k-1][l-1]+1,dp[i][j]);
40             }
41         }
42         printf("%d\n",dp[n][m]*2);
43     }
44     return 0;
45 }

 

zoj 1425 最大交叉匹配

标签:style   blog   http   io   color   ar   os   sp   for   

原文地址:http://www.cnblogs.com/xiong-/p/4089987.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!