ZOJ 1425 Crossed Matchings(动态规划)

来源:互联网 发布:淘宝店铺刷流量有用吗 编辑:程序博客网 时间:2024/04/25 23:37

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>


0 0
原创粉丝点击