POJ 1692 Crossed Matchings (DP), pojmatchings

Source: Internet
Author: User
Tags integer numbers

POJ 1692 Crossed Matchings (DP), pojmatchings

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

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 <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", & 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 ;}



A typical DP question is best created

Zju search dp question summary favorites
|
Search:
50 search questions

1002 Fire Net-OK
1004 Anagrams by Stack-OK
1005 Jugs-OK
1008 Gnome Tetravex-OK but not ac
1091 Knight Moves-OK
1101 Gamblers-OK
1204 Additive equations
1221 Risk
1230 Legendary Pokemon
1249 Pushing Boxes
1364 Machine Schedule
1368 BOAT
1406 Jungle Roads --- OK

1411 Anniversary
1453 Surround the Trees convex hull
1516 Uncle Tom's Inherited Land --- OK
1525 Air Raid may be more suitable for Network Flow Calculation
1586 QS Network
1602 Multiplication Puzzle dp
1649 Rescue
1671 Walking Ant ---- OK

1711 Sum It Up dfs ---- OK
1901 A Star not a Tree? The search may be a bit controversial (no matter)
1940 Dungeon Master ---- OK
2100 Seeding ---- OK
2110 Tempter of the Bone
2140 Ball
(27)
The above questions may be relatively simple. Just practice basic algorithms.
========================================================== ========================================================
The following questions are not easy to do.
Difficulties & classic
1003 Crashing Balloon
1015 Fishing Net perfect image
1144 Robbery
1149 Dividing up
1161 Gone Fishing
1197 Sorting Slides
1217 Eight
1228 Farewell, My Friend
1237 Fans and Gems
1455 Schedule Problem
1456 Minimum Transport Cost graph Shortest Path to save
1492 Maximum Clique classical Graph Theory Algorithms-the largest group
1600 Market Place
1605 One-way Traffic
1568 WishingBone's Room Plan
1742 Gap has no idea so far. It's hard !!!
1743 Concert Hall Scheduling
1827 The Game of 31
1855 Maze
1903 Jogging Trails China mail routes
1909 Square classic dfs.
2064 Bomberman-Just Search! Classic!
2094 Max Angle Calculation ry + game
2125 Rocket Mania
2126 Rocket Mania Plus
2127 Zuma
2128 Seven Seas
2129 Mummy Maze
2142 Light The Square
(24 channels)
========================================================== ===
Dp
50 dp questions

1499 Increasing Sequenc ...... remaining full text>

Acm performs C ++

The report on solving this problem is relatively old, and now the g ++ version used by Online Judge is quite new, so you need to make some modifications to the Code in the report.
Change # include <iostream. h> to # include <iostream>.
Add using namespace std after # include;
Submit with g ++, you can use

Solution:
This is a classic dynamic planning question.
Defines the number of matching segments that Match [I] [j] is the maximum number of matching segments before the first row and the first j of the second row.
Therefore, it is not difficult to obtain the dynamic equation:
For I = 1 to length (s1) do
For j = 1 to length (s2) do
Begin
Match [I] [j] = MAX {Match [I-1] [j], Match [I] [J-1]}
If s1 [I] <> s2 [j] then
Begin
For l1 = I-1 downto 1 do if s1 [l1] = s2 [j] then
For l2 = J-1 downto 1 do if s1 [I] = s2 [l2] then
Begin
Match [I] [j] = MAX {Match [I] [j], Match [l1-1] [l2-1] + 2}
Break; {here the Break is based on a monotonicity of Match}
End;
End;
End;
The time complexity of the algorithm is O (Sqr (length (s1) * lengths (2 )))
In extreme cases, length (s1) = length (s2) = 100. The calculation workload will reach 10 ^ 8. The time limit is 1 s. Is there a better way? Yes.
Through observation, we found that in the original algorithm, the last two layers made repeated searches many times. What can we do through preprocessing? Now that we have mentioned this, we can explain
Define f [I] [j] = Max {k | s1 [I] = s2 [k]; k <j ;! T> k t <j s1 [I] = s2 [t]}
G [I] [j] = Max {k | s1 [k] = s2 [I]; k <j ;! T> k t <j s2 [I] = s1 [t]}
Therefore, it is not uncommon to create a dynamic equation:
For I = 1 to length (s1) do
For j = 1 to length (s2) do
Match [I] [j] = MAX {Match [I-1] [j], Match [I] [J-1], match [f [I] [j] [g [j] [I] + 2}
The new time complexity is O (N * N). the time complexity of preprocessing is also O (N * N ). So the total time complexity is O (N * N). At this point, we have successfully completed this question.

Data Structure: Array

Time-Space Analysis:
Time: O (N * N)
Space: O (N * N)

Source program:

# Include <iostream>
# Include <string. h>
# Include <limits. h>
Using namespace std;
# Define MaxN 101 // maximum number of elements

Int s1 [MaxN], s2 [MaxN], match [MaxN] [MaxN], f [MaxN] [MaxN], g [MaxN] [MaxN];
/*
S1 [MaxN]: element of the first line
S2 [MaxN]: Second Line Element
Match [MaxN] [MaxN]: The report says... the remaining full text>

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.