zoj1425 Crossed Matchings

【题解】:

【代码】:

 1 #include <iostream>
2 #include <string.h>
3 #include <stdio.h>
4 using namespace std;
5
6 int dp[105][105];
7 int N1,N2;
8 int a[105],b[105];
9 int main(){
10 int M;
11 scanf("%d",&M);
12 while(M--){
13 scanf("%d%d",&N1,&N2);
14 for(int i=1;i<=N1;i++) scanf("%d",&a[i]);
15 for(int i=1;i<=N2;i++) scanf("%d",&b[i]);
16
17 memset(dp,0,sizeof(dp));
18 for(int i=1;i<=N1;i++){
19 for(int j=1;j<=N2;j++){
20 if (a[i]==b[j]) dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
21 if (a[i]!=b[j]) {
22 int mati=-1,matj=-1;
23 for(int p=j-1;p>=1;p--){//在第二行搜索可能匹配的位置
24 if (b[p]==a[i]) {
25 mati=p;break;
26 }
27 }
28 for(int p=i-1;p>=1;p--){//在第一行搜索匹配b的位置
29 if (a[p]==b[j]){
30 matj=p;break;
31 }
32 }
33 dp[i][j]=max(dp[i][j],dp[i-1][j]);
34 dp[i][j]=max(dp[i][j],dp[i][j-1]);
35 if(mati>0 && matj>0) dp[i][j]=max(dp[i][j],dp[matj-1][mati-1]+2);
36 }
37 }
38 }
39
40 cout<<dp[N1][N2]<<endl;
41 }
42 return 0;
43 }

时间: 2024-08-07 19:11:58

zoj1425 Crossed Matchings的相关文章

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 segmen

poj 1692 Crossed Matchings(DP)

Crossed Matchings Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2717 Accepted: 1763 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

ZOJ 1425 Crossed Matchings(动态规划)

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1425 Crossed Matchings Time Limit: 2 Seconds      Memory Limit: 65536 KB There are two rows of positive integer numbers. We can draw one line segment between any two equal numbers, with val

POJ1692 Crossed Matchings

Time Limit: 1000MS     Memory Limit: 10000K Total Submissions: 2738   Accepted: 1777 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 t

ZOJ 1425 Crossed Matchings(LCS变形)

题目大意: 就是说给你两行数字,然后对于每一行的相同的数字进行连线,每次连接成功后,就算是1次成功,求最大的成功次数.当然,要成功的话,也是要满足一定的条件的: 1.在连接的过程中,不能用两根线连接了4个相同数字. 2.一个数字不能发出两条线. 解题思路: 刚看到这个题,以为是图论的知识,感觉自己拿不下,,(有点像是什么二分图匹配吧,用两个颜色给相邻的顶点染色,求最多能有多少种这样的匹配,,逃) 其实呢,这个有点像LCS,,,我第一次也没看出来,看了题解后,才明白的. 我们规定这样的状态: dp

POJ百道水题列表

以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight Moves1101 Gamblers1204 Additive equations 1221 Risk1230 Legendary Pokemon1249 Pushing Boxes 1364 Machine Schedule1368 BOAT1406 Jungle Roads1411 Annive

poj 动态规划题目列表及总结

此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276,1322, 1414, 1456, 1458, 1609, 1644, 1664, 1690, 1699, 1740(博弈),1742, 1887, 1926(马尔科夫矩阵,求平衡), 1936, 1952, 1953, 1958, 1959, 1962, 1975,

zoj题目分类

饮水思源---zoj 转载自:http://bbs.sjtu.edu.cn/bbscon,board,ACMICPC,file,M.1084159773.A.html 注:所有不是太难的题都被归成了“简单题”,等到发现的时候已经太晚了,我太死脑筋 了……:( 有些题的程序我找不到了,555……:( SRbGa的题虽然都很经典……但是由于其中的大部分都是我看了oibh上的解题报告后做 的,所以就不写了…… 题目排列顺序没有规律……:( 按照个人感觉,最短路有的算做了DP,有的算做了图论. 有些比较

经典动态规划总结

本文持续更新…… 1 给定两组序列 求上下匹配的最大值(POJ1692 Crossed Matchings) 题意:给出两行数,求上下匹配的最多组数是多少. 匹配规则: 1 匹配对的数字必须相同 2 每个匹配必须有且只能有一个匹配与之相交叉,且相交叉的两组匹配数字必须不同 3 一个数最多只能匹配一次 思路: dp[i][j]表示上面取i个数,下面取j个数的最大匹配数 1)若上面不匹配或下面不匹配,则dp[i][j] = max(dp[i - 1][j], dp[i][j - 1] ) 2)若a[