Description There is both rows of positive integer numbers. We can draw one line segment between any and equal numbers, with values R, if one of them are located in the first row and The other one was located in the second row. We call the 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 matching segments can is drawn from a number. For example, the following matchings is 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 was the number m, which is the number of the 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 is the numbers on the first row. The third line contains N2 integers which is the numbers on the second row. All numbers is positive integers less than 100.Output Output should has one separate line for each test case. The maximum number of matching segments for each test case should is written in one separate line.Sample Input
Sample Output 608
Test instructions: The same number can be connected but must intersect with different numbers. Ask the most possibilities DP[I][J] represents the maximum possible of the first I and the second row of the front j of a row.
|
#include <limits.h>using namespace Std;int a[110],b[110];int dp[110][110];int n,m,t;int main () {int k1,k2; scanf ("%d", &t); while (t--) {scanf ("%d%d", &n,&m); for (int i=1;i<=n;i++) scanf ("%d", &a[i]); for (int j=1;j<=m;j++) scanf ("%d", &b[j]); 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]), or//the maximum value of the state of the dp[i][j] that is equal to if (A[i]!=b[j]) {for (k1=i;k1>=1;k ) {if (b[j]==a[k1]) break; } for (k2=j;k2>=1;k2--) {if (A[i]==b[k2]) Break } if (K1&&K2) Dp[i][j]=max (dp[i][j],dp[k1-1][k2-1]+2);//update dp[i][j] }}} printf ("%d\n", Dp[n][m]); } return 0;}
POJ 1692 crossed matchings (DP)