POJ

来源:互联网 发布:app读书软件排行 编辑:程序博客网 时间:2024/06/04 00:35

Crossed Matchings

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

36 61 3 1 3 1 33 1 3 1 3 14 41 1 3 3 1 1 3 3 12 111 2 3 3 2 4 1 5 1 3 5 103 1 2 3 2 4 12 1 5 5 3 

Sample Output

608


题意:给出两串数字,将相同的数字连线,连线必须相交,但是相同的数字的线不能相交,看样例很好理解。


解题思路:慢慢练,相信以后也可以自己想到递推式!!用dp[i][j]代表第一行第1~i个和第二行第1~j个之间的最多连接数。先不考虑将a[i],b[j]连线的情况,肯定有dp[i][j]=max(dp[i][j-1],max(dp[i-1][j],dp[i-1][j-1]));然后再考虑连线的情况。如果a[i]==a[j],那么将a[i]和b[j]连线是毫无意义的,因为不可能产生相交,所以要考虑不相等的情况,这个时候应该怎么办呢。这里用到了贪心的思想,将第二行与a[i]最近相等的相连,和第一行与b[j]最近相等的相连(其实很容易想到,这样才能保证前面最长,越长越多相交的 可能性)。这样就有递推式dp[i][j]=max(dp[i][j],dp[k1-1][k2-1]+2);k1为与b[j]相等且最近那个,k2为与a[i]相等且最近那个。详见代码。


#include<iostream>#include<memory.h>#include<string>#include<algorithm>using namespace std;const int MAXN=105;int N,M;int dp[MAXN][MAXN];int a[MAXN];int b[MAXN];int main(){    int t;    scanf("%d",&t);    while(t--){        scanf("%d%d",&N,&M);        for(int i=1;i<=N;i++)            scanf("%d",&a[i]);        for(int i=1;i<=M;i++)            scanf("%d",&b[i]);        memset(dp,0,sizeof(dp));        for(int i=1;i<=N;i++)            for(int j=1;j<=M;j++){                dp[i][j]=max(dp[i][j-1],max(dp[i-1][j],dp[i-1][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);                }            }        printf("%d\n",dp[N][M]);    }    return 0;}