ZOJ 1425 Crossed Matchings(LCS变形)

题目大意:

  就是说给你两行数字,然后对于每一行的相同的数字进行连线,每次连接成功后,就算是1次成功,求最大的成功次数。当然,要成功的话,也是要满足一定的条件的:

  1.在连接的过程中,不能用两根线连接了4个相同数字。

  2.一个数字不能发出两条线。

解题思路:

  刚看到这个题,以为是图论的知识,感觉自己拿不下,,(有点像是什么二分图匹配吧,用两个颜色给相邻的顶点染色,求最多能有多少种这样的匹配,,逃)

其实呢,这个有点像LCS,,,我第一次也没看出来,看了题解后,才明白的。

  我们规定这样的状态: dp[i][j] 表示第一行前i个数字和第二行前j个数字的最大匹配数目.

            由LCS的状态转移方程知道:dp[i][j] = max( dp[i-1][j],dp[i][j-1]);

  那么我们由题目要求的定义知道,如果出现第1行的第i个字符和第二行的第j个字符不同的时候,也就是说a[i]!=b[j],那么,我们开始先定住a[i],从b[j]开始往前找,如果找到了a[i]==b[k]的位置,那就说明,在a[i]和b[k]之间就会有一条连线了。这时候,由于一个元素只能发出一条连接,我们查找结束。

  接下来,我们定住b[j]按照同样的方法来在a[l]中去找,找到第一个a[l]==b[j]的地方,我们就结束掉。分别记录这两次查找的过程中a[l]==b[j]和a[i]==b[k]的l和k的位置,更新dp[i][j]用,因为但凡有了这两次连线,对于dp[i][j]来说就会贡献2条线。。。如果我们在两次查找的过程中,有某一次或者两次的l和k的位置都没有出现的话,我们就说明dp[i][j]并没有得到更新。

代码:

 1 # include<cstdio>
 2 # include<iostream>
 3 # include<fstream>
 4 # include<algorithm>
 5 # include<functional>
 6 # include<cstring>
 7 # include<string>
 8 # include<cstdlib>
 9 # include<iomanip>
10 # include<numeric>
11 # include<cctype>
12 # include<cmath>
13 # include<ctime>
14 # include<queue>
15 # include<stack>
16 # include<list>
17 # include<set>
18 # include<map>
19
20 using namespace std;
21
22 const double PI=4.0*atan(1.0);
23
24 typedef long long LL;
25 typedef unsigned long long ULL;
26
27 # define inf 999999999
28 # define MAX 123
29
30 int a[MAX];
31 int b[MAX];
32 int dp[MAX][MAX];
33
34 int main(void)
35 {
36     int t;cin>>t;
37     while ( t-- )
38     {
39         int n,m;
40         cin>>n>>m;
41         for ( int i = 1;i <= n;i++ )
42             cin>>a[i];
43         for ( int i = 1;i <= m;i++ )
44             cin>>b[i];
45         int k,l;
46         for ( int i =1;i<= n;i++ )
47         {
48             for ( int j = 1;j <= m;j++ )
49             {
50                 dp[i][j] = max( dp[i-1][j],dp[i][j-1] );
51                 if ( a[i]!=b[j] )
52                 {
53                     for ( k = j-1;k >= 1;k-- )
54                     {
55                         if ( b[k]==a[i] )
56                             break;
57                     }
58                     for ( l = i-1;l >= 1;l-- )
59                     {
60                         if ( a[l]==b[j] )
61                             break;
62                     }
63
64                     if ( l!=0&&k!=0 )
65                     {
66                         dp[i][j] = max(dp[i][j],dp[l-1][k-1]+2 );
67                     }
68                 }
69             }
70         }
71         cout<<dp[n][m]<<endl;
72
73     }
74
75
76     return 0;
77 }
时间: 2024-08-27 05:09:01

ZOJ 1425 Crossed Matchings(LCS变形)的相关文章

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

zoj 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 values r, if one of them is located in the first row and the other on

poj 1080 (LCS变形)

Human Gene Functions 题意: LCS: 设dp[i][j]为前i,j的最长公共序列长度: dp[i][j] = dp[i-1][j-1]+1;(a[i] == b[j]) dp[i][j] = max(dp[i][j-1],dp[i-1][j]); 边界:dp[0][j] = 0(j<b.size) ,dp[i][0] = 0(i< a.size); LCS变形: 设dp[i][j]为前i,j的最大价值: value(x, y)为比较价值: dp[i][j] = max(d

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

poj1080——lcs变形,dp

poj1080——lcs变形,dp Human Gene Functions Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17610   Accepted: 9821 Description It is well known that a human gene can be considered as a sequence, consisting of four nucleotides, which are simpl

POJ 1080 Human Gene Functions(求两字符串相似度:LCS变形)

POJ 1080 Human Gene Functions(求两字符串相似度:LCS变形) http://poj.org/problem?id=1080 题意: HDU1080 给你两个由字符A,C,G,T构造的字符串s1和s2, 现在你可以在这两个字符串中插入空格, 使得两串长相等(但是不能使得s1的空格对应s2的空格位置). 然后给你s1的特定字符对应s2中特定字符所能获得的分数矩阵: 问你最后两个字符串所能获得的最大分数是多少? 分析: 本题很类似于求字符串最短编辑距离或者求字符串LCS的

UVA-1625-Color Length(DP LCS变形)

Color Length(UVA-1625)(DP LCS变形) 题目大意 输入两个长度分别为n,m(<5000)的颜色序列.要求按顺序合成同一个序列,即每次可以把一个序列开头的颜色放到新序列的尾部. https://odzkskevi.qnssl.com/a68cbd3e27f46b4f02ea12b7b1a1abca 然后产生的新序列中,对于每一个颜色c,都有出现的位置,L(c)表示最小位置和最大位置之差,求L(c)总和最小的新序列. 分析 LCS 是公共上升子序列,在动态转移的过程中,考虑

Advanced Fruits(HDU 1503 LCS变形)

Advanced Fruits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2358    Accepted Submission(s): 1201Special Judge Problem Description The company "21st Century Fruits" has specialized in cr