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

POJ1692:Crossed Matchings

时间:2015-05-12 21:06:37      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:poj

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


引用下别人的解释吧,自认不能比他写的更详细了。。。

题意:给出两行数,求上下匹配的最多组数是多少。 
匹配规则 
1,匹配对的数字必须相同 
2.每个匹配必须有且只能有一个匹配与之相交叉,且相交叉的两组匹配数字必须不同 
2,一个数最多只能匹配一次 

分析:用dp[i][j]表示第一行取i个数,第二行取j个数字的最多匹配项 
对于某个dp[i][j]: 
1.不匹配第一行i个,或不匹配第二行第j个:dp[i][j]=Max(dp[i-1][j],dp[i][j-1]) 
2.如果a[i]==b[j],不产生新匹配,匹配结果为1的值 
3.若a[i]!=b[j]: 
a.则第一行从i往前扫,直到扫到第一个a[k1]==b[j](k1 b.同理,第二行从j往前扫,直到扫到第一个b[k2]==a[i](k2 若找不到这样的k1,k2则不能才产生新匹配,跳过 
若存在这样的k1,k2,此时匹配(a[i],b[k2])、(a[k1],b[j])匹配, 
才生新的匹配情形,匹配数量为:dp[k1-1][k2-1]+2. 
最优dp[i][j]=Max(1,2,3); 

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std;
#define ls 2*i
#define rs 2*i+1
#define up(i,x,y) for(i=x;i<=y;i++)
#define down(i,x,y) for(i=x;i>=y;i--)
#define mem(a,x) memset(a,x,sizeof(a))
#define w(a) while(a)
#define LL long long
const double pi = acos(-1.0);
#define N 505
#define mod 19999997
#define INF 0x3f3f3f3f
#define exp 1e-8

int t,n,m,a[105],b[105],dp[105][105];

int main()
{
    int i,j,k;
    scanf("%d",&t);
    w(t--)
    {
        scanf("%d%d",&n,&m);
        up(i,1,n)
        scanf("%d",&a[i]);
        up(i,1,m)
        scanf("%d",&b[i]);
        mem(dp,0);
        up(i,2,n)
        {
            up(j,2,m)
            {
                dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
                if(a[i]!=b[j])
                {
                    int k1=0,k2=0;
                    down(k1,i-1,1)
                    if(a[k1]==b[j])
                    break;
                    down(k2,j-1,1)
                    if(b[k2]==a[i])
                    break;
                    if(k1&&k2)
                    dp[i][j]=max(dp[i][j],dp[k1-1][k2-1]+2);
                }
            }
        }
        printf("%d\n",dp[n][m]);
    }

    return 0;
}


POJ1692:Crossed Matchings

标签:poj

原文地址:http://blog.csdn.net/libin56842/article/details/45674139

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