ZOJ 1425 Crossed Matchings(动态规划)

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1425

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

题意:

求上下匹配组数目。

规则:

1、匹配对的数必须相同。

2、一个数最多只有一个匹配。

3、一个匹配有且只能有一个匹配与之相交叉,且相交叉的两个匹配数 字不能相同。

解题思路:

用dp[i][j]表示第一行前i个数和第二行前j个数满足条件的匹配数。

*1、如果不匹配第一行第i个,或者不匹配第二行第j个则 dp[i][j]=max(dp[i][j-1],dp[i-1][j])

*2、如果a[i]==b[j]则不产生新匹配,匹配数与*1一样

*3、如果a[i]!=b[j]:

在第一行中往左搜,直到a[k1]==b[j];在第二行中往左搜,直到b[k2]==a[i],然后就有dp[i][j]=max(dp[i][j],dp[k1-1][k2-1]+2)

<span style="font-size:18px;">#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=1010;
int dp[maxn][maxn];
int a[maxn],b[maxn];
int main()
{
    int N,n,m;
    cin>>N;
    while(N--)
    {
        cin>>n>>m;
        for(int i=1;i<=n;i++) cin>>a[i];
        for(int i=1;i<=m;i++) cin>>b[i];
        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]);
                if(a[i]!=b[j])
                {
                    int k1,k2;
                    for(k1=i-1;k1>=1;k1--)
                        if(a[k1]==b[j]) break;
                    for(k2=j-1;k2>=1;k2--)
                        if(b[k2]==a[i]) break;
                    if(k1&&k2){
                        dp[i][j]=max(dp[i][j],dp[k1-1][k2-1]+2);
                    }
                }
            }
        }
        cout<<dp[n][m] << endl;
    }
    return 0;
}</span>

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-06 16:18:23

ZOJ 1425 Crossed Matchings(动态规划)的相关文章

ZOJ 1425 Crossed Matchings(LCS变形)

题目大意: 就是说给你两行数字,然后对于每一行的相同的数字进行连线,每次连接成功后,就算是1次成功,求最大的成功次数.当然,要成功的话,也是要满足一定的条件的: 1.在连接的过程中,不能用两根线连接了4个相同数字. 2.一个数字不能发出两条线. 解题思路: 刚看到这个题,以为是图论的知识,感觉自己拿不下,,(有点像是什么二分图匹配吧,用两个颜色给相邻的顶点染色,求最多能有多少种这样的匹配,,逃) 其实呢,这个有点像LCS,,,我第一次也没看出来,看了题解后,才明白的. 我们规定这样的状态: dp

zoj 1425 最大交叉匹配

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 on

POJ 1692 Crossed Matchings(DP)

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 segmen

poj 1692 Crossed Matchings(DP)

Crossed Matchings Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2717 Accepted: 1763 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

POJ1692 Crossed Matchings

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 t

[ZOJ 3662] Math Magic (动态规划+状态压缩)

先贴代码,晚上回去说 1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4 #include <cmath> 5 #include <map> 6 #include <iterator> 7 #include <vector> 8 using namespace std; 9 typedef long long LL; 10 11 int n,m

zoj1425 Crossed Matchings

[题解]: [代码]: 1 #include <iostream> 2 #include <string.h> 3 #include <stdio.h> 4 using namespace std; 5 6 int dp[105][105]; 7 int N1,N2; 8 int a[105],b[105]; 9 int main(){ 10 int M; 11 scanf("%d",&M); 12 while(M--){ 13 scanf(

zoj题目分类

饮水思源---zoj 转载自:http://bbs.sjtu.edu.cn/bbscon,board,ACMICPC,file,M.1084159773.A.html 注:所有不是太难的题都被归成了“简单题”,等到发现的时候已经太晚了,我太死脑筋 了……:( 有些题的程序我找不到了,555……:( SRbGa的题虽然都很经典……但是由于其中的大部分都是我看了oibh上的解题报告后做 的,所以就不写了…… 题目排列顺序没有规律……:( 按照个人感觉,最短路有的算做了DP,有的算做了图论. 有些比较

POJ百道水题列表

以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight Moves1101 Gamblers1204 Additive equations 1221 Risk1230 Legendary Pokemon1249 Pushing Boxes 1364 Machine Schedule1368 BOAT1406 Jungle Roads1411 Annive