北大ACM poj1692 Crossed Matchings(动态规划)
来源:互联网 发布:淘宝客推广怎么弄 编辑:程序博客网 时间:2024/04/25 19:35
Crossed Matchings
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 2298 Accepted: 1475
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
/*题意:给出两行数,求上下匹配的最多组数是多少。匹配规则1,匹配对的数字必须相同2.每个匹配必须有且只能有一个匹配与之相交叉,且相交叉的两组匹配数字必须不同2,一个数最多只能匹配一次方法:DP分析:用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.最优dp[i][j]=Max(1,2,3);*/#include<stdio.h>#include<stdlib.h>int dp[102][102];int a[102],b[102];int n,m;int max(int a,int b){//求ab中的较大值 return a>b?a:b;}int main(){ int i,j,t,k1,k2; scanf("%d",&t);//输入测试的组数 while(t--){ scanf("%d%d",&n,&m);//输入两个数字串的长度 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));//初始化 for(i=2;i<=n;i++) for(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-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]); }}
- 北大ACM poj1692 Crossed Matchings(动态规划)
- POJ1692————Crossed Matchings(动态规划)
- POJ1692 Crossed Matchings(dp)
- 【动态规划】Crossed Matchings
- ZOJ1425 POJ1692 Crossed Matchings
- POJ1692:Crossed Matchings
- POJ1692 Crossed Matchings
- POJ1692 Crossed Matchings DP
- ZOJ 1425 Crossed Matchings(动态规划)
- poj 1692 Crossed Matchings 动态规划
- poj 1692 Crossed Matchings 动态规划
- Crossed Matchings——动态规划
- poj1692 Crossed Matchings(dp,最长公共子序列变形,好题)
- POJ 1692 Crossed Matchings(DP)
- PKU1692 Crossed Matchings
- POJ 1692 Crossed Matchings
- pku1692 Crossed Matchings
- poj 1692 Crossed Matchings
- 北斗资料
- 一个二维求三点一线算法
- Linux驱动修炼之道-SPI驱动框架源码分析(中)
- 日期操作方法(总结Date,Calender类)
- 老牌手机的黄昏
- 北大ACM poj1692 Crossed Matchings(动态规划)
- Linux驱动修炼之道-SPI驱动框架源码分析(下)
- Access数据库的模糊查询到底是用*还是%
- 扶贫的美国名校
- 让 Putty 保存密码,自动登陆的四种方法
- ST、SC、FC、LC光纤接头区别
- MongoDB Java Driver 源码分析收藏
- boost 资源
- 广告管理系统