poj1692 Crossed Matchings(dp,最长公共子序列变形,好题)
来源:互联网 发布:c#网络编程视频 编辑:程序博客网 时间:2024/04/18 09:21
Crossed Matchings
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 2838 Accepted: 1840
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 should cross exactly one b-matching segment, where a != 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.
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 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 should have one separate line for each test case. The maximum number of matching segments for each test case should 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
Source
Tehran 1999
参考博客链接
题意:
给出两行数,求上下匹配的最多组数是多少。
匹配规则
1.匹配对的数字必须相同
2.每个匹配必须有且只能有一个匹配与之相交叉,且相交叉的两组匹配数字必须不同
3.一个数最多只能匹配一次
题解:
一开始我以为是个二分匹配的题目,后来想了好久不知道怎么处理第二个条件。
这题其实是动态规划题。
分析:用dp[i][j]表示第一行取i个数,第二行取j个数字的最多匹配项
对于某个dp[i][j]:
1.不匹配第一行i个,或不匹配第二行第j个:dp[i][j]=Max(dp[i-1][j],dp[i][j-1])
2.如果a[i]==b[j],不产生新匹配,匹配结果为1的值
3.若a[i]!=b[j]:
a.则第一行从i往前扫,直到扫到第一个a[k1]==b[j](k1 b.同理,第二行从j往前扫,直到扫到第一个b[k2]==a[i](k2 若找不到这样的k1,k2则不能才产生新匹配,跳过
若存在这样的k1,k2,此时匹配(a[i],b[k2])、(a[k1],b[j])匹配,
才生新的匹配情形,匹配数量为:dp[k1-1][k2-1]+2。
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<vector>#include<queue>#include<stack>using namespace std;#define rep(i,a,n) for (int i=a;i<n;i++)#define per(i,a,n) for (int i=n-1;i>=a;i--)#define pb push_back#define fi first#define se secondtypedef vector<int> VI;typedef long long ll;typedef pair<int,int> PII;const int inf=0x3fffffff;const ll mod=1000000007;const int maxn=100+10;int n,m;int a[maxn],b[maxn];int d[maxn][maxn];int main(){ int cas; scanf("%d",&cas); while(cas--) { scanf("%d%d",&n,&m); rep(i,1,n+1) scanf("%d",&a[i]); rep(i,1,m+1) scanf("%d",&b[i]); memset(d,0,sizeof(d)); rep(i,2,n+1) rep(j,2,m+1) { d[i][j]=max(d[i][j-1],d[i-1][j]); if(a[i]==b[j]) continue; else { int p1=0,p2=0; for(int k=i-1;k>0;k--) { if(a[k]==b[j]) { p1=k; break; } } for(int l=j-1;l>0;l--) { if(b[l]==a[i]) { p2=l; break; } } if(p1&&p2) d[i][j]=max(d[i][j],d[p1-1][p2-1]+2); } } printf("%d\n",d[n][m]); } return 0;}
0 0
- poj1692 Crossed Matchings(dp,最长公共子序列变形,好题)
- 【poj 1692】Crossed Matchings (最长公共子序列变形)
- POJ1692 Crossed Matchings DP
- POJ1692 Crossed Matchings(dp)
- ZOJ1425 POJ1692 Crossed Matchings
- POJ1692:Crossed Matchings
- POJ1692 Crossed Matchings
- 比较好的最长公共子序列的变形
- DP专辑之最长公共子序列及其变形
- hdu1503 Advanced Fruits(DP 最长公共子序列变形)
- 最长上升子序列 最长公共子序列 最长公共子串 数字三角形 等简单DP以及变形
- ZOJ1425 Crossed Matchings dp
- Crossed Matchings zoj1425 dp
- 最长公共子序列--DP
- DP---最长公共子序列
- 最长公共子序列dp
- DP 最长公共子序列
- DP 最长公共子序列
- 线性回归问题
- HeadFirstSQL学习笔记(四)
- array.sort的深入理解
- 2017.2.3 Java有感4
- [11]Linux网络编程模型
- poj1692 Crossed Matchings(dp,最长公共子序列变形,好题)
- 教女朋友学Python(6)——挖了坑是要填的
- 17 - 05 - 12 C++初窥
- C++中的unique函数
- 字符设备的子设备的识别方法一
- 神经网络与深度学习(二)cpp-c++BP神经网络
- 详解Unity3D Shader之Shader Lab框架
- CCNA 1IPV4地址划分
- Linux系统编程学习01