POJ 1692 Crossed Matchings (DP)

Source: Internet
Author: User
Tags integer numbers

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 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 fZ limit? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> keys + keys =" http://www.2cto.com/uploadfile/Collfiles/20140817/20140817093606172.jpg "alt =" \ ">
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 shocould have one separate line for each test case. The maximum number of matching segments for each test case shocould 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

The same number can be connected but must be connected to different numbers. Maximum possibility

Dp [I] [j] indicates the maximum possibility of the first j of the first and second rows.


# Include
 
  
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", & 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]); // if (a [I]! = B [j]) {for (k1 = I; k1> = 1; k1 --) {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 ;}
 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.