crossed Matchings
| Time Limit: 1000MS |
|
Memory Limit: 10000K |
| Total Submissions: 2717 |
|
Accepted: 1763 |
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.
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: Give two rows to find out what the maximum number of groups to match up and down. Match Rule 1, the number of matching pairs must be the same 2. Each match must have and only one match to intersect, and the intersection of the two sets of matching numbers must be different 2, a number can only match once method: DP analysis: Using DP[I][J] to represent the first row to take I number, The second row takes the maximum number of occurrences of j digits for a dp[i][j]:1. Does not match the first row I, or does not match the second line, J: Dp[i][j]=max (Dp[i-1][j],dp[i][j-1]) 2. If A[I]==B[J] does not produce a new match, The match result is a value of 1 of 3. If a[i]!=b[j]:a. The first line is swept forward from I until sweep to the first a[k1]==b[j] (K1 B. Similarly, the second line sweeps forward from J until it sweeps to the first b[k2]==a[i] (K2 If no such K1 is found, K2 is not able to produce a new match, skipping if there is such a k1,k2, this time match (A[i],b[k2]), (A[k1],b[j]) match, only the new match case, the matching quantity is: dp[k1-1][k2-1]+2. Best Dp[i][j]=max ); */#include <cstdio> #include <cstdlib> #include <cstring>int dp[102][102];int a[102],b[102];int N, M;int max (int a,int b) {//ask for a larger value in AB return a>b?a:b;} int main () {int i,j,t,k1,k2; scanf ("%d", &t);//input test group number while (t--) {scanf ("%d%d", &n,&m);//Enter the length of the two-digit string for (i=1;i<=n;i++) scanf ("%d", &a[i]); for (j=1;j<=m;j++) scanf ("%d", &b[j]); Memset (Dp,0,sizeof (DP));//Initialize for (i=2;i<=n;i++) for (j=2;j<=m;j++) {Dp[i][j]=max (dp[ I-1][j],dp[i][J-1]); /Select the previous largest if (A[i]!=b[j]) {for (k1=i-1;k1>=1;k1--) if (A[k1]==b[j] ) break; for (k2=j-1;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); }} printf ("%d\n", Dp[n][m]); }return 0;}
Poj 1692 crossed matchings (DP)