Poj 1692 crossed matchings (DP)

Source: Internet
Author: User
Tags integer numbers

There are two rows. A [N1] B [n2] has the N1 N2 number. When the first row and the second row have the same number, they can connect to only one row. find the number of lines at most so that each line has at least one line at the same time.

Make d [I] [J] indicate the maximum number of front I numbers of A and the maximum number of front J numbers of J.

When a [I] = B [J], it is certainly not connected with other lines, so d [I] [J] = max (d [I-1] [j], d [I] [J-1])

When a [I]! = B [J], if you can find a number in the first row x <I, find a number in the second row Y <j makes a [x] = B [J] B [y] = A [I] Then d [I] [J] = max (d [x] [Y] + 2, d [I-1] [J], d [I] [J-1]) if d [I] [J] = max (d [I-1] [J], d [I] [J-1])

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int N = 105;int a[N], b[N], d[N][N], la, lb, cas;int main(){    scanf ("%d", &cas);    while (cas--)    {        scanf ("%d%d", &la, &lb);        for (int i = 1; i <= la; ++i)            scanf ("%d", &a[i]);        for (int j = 1; j <= lb; ++j)            scanf ("%d", &b[j]);        for (int i = 1; i <= la; ++i)            for (int j = 1; j <= lb; ++j)            {                d[i][j] = max (d[i][j - 1], d[i - 1][j]);                int x, y;                for (x = i - 1; x >= 1; --x)                    if (a[x] == b[j]) break;                for (y = j - 1; y >= 1; --y)                    if (a[i] == b[y]) break;                if (x && y && a[i] != b[j])                    d[i][j] = max (d[x - 1][y - 1] + 2, d[i][j]);            }        printf ("%d\n", d[la][lb]);    }    return 0;}

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 shold cross exactly one B-matching segment, where! = 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 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

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.