ACM: 动态规划 poj 1692

                                                                                                                                  Crossed Matchings
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.
ACM: <wbr>动态规划 <wbr>poj <wbr>1692

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.
ACM: <wbr>动态规划 <wbr>poj <wbr>1692

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

3

6 6
 1 3 1 3 1 3
 3 1 3 1 3 1
4 4
 1 1 3 3
 1 1 3 3
12 11
 1 2 3 3 2 4 1 5 1 3 5 10
 3 1 2 3 2 4 12 1 5 5 3

Sample Output

6

0
8

题意: 两个序列, 相同的匹配为一对, 要求对数最大.
      匹配要遵循两个规则: 1. 每条匹配的连线, 都要与并且是只有另一条匹配的连线相交.
                        2. 一个数只可以有一个匹配.

解题思路:
        1. 动态规划题: 规划的状态 dp[i][j]: 表示第一行前i个数与第二行前j个的最大匹配数.
          问题解析:
               当a[i] == b[j]时, dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
               当a[i] != b[j]时,
                 第一行的序列, 从第i个之前的前p处 和 第二行序列, 从第j个之前的前q处,
                 使之满足 a[p-1] == b[j] && b[q-1] == a[i];
                 dp[i][j] = max(dp[i][j],dp[p-1][q-1]+2); (+2: 因为同时有两个匹配成立).
         2. 优化的思路: 枚举p , q是算法的时间复杂度: O(n^2*m^2);
           优化解析: (保证了相交只有一条. (隐含条件) )
               假设: q[i][j]: 表示数组b[j]之前的数,与a[i]匹配的数的最大位置.
               同理: p[j][i]: 表示数组a[i]之前的数,与b[j]匹配的数的最大位置.

代码1:  times: 16MS
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
#define MAX 102

int n, m;
int a[MAX], b[MAX];
int dp[MAX][MAX];

inline int max(int a,int b)
{
 return a > b ? a : b;
}

int main()
{
 int i, j;
// freopen("input.txt","r",stdin);
 int caseNum;
 scanf("%d",&caseNum);
 while(caseNum--)
 {
  memset(dp,0,sizeof(dp));
  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]);

  for(i = 1; i <= n; ++i)
  {
   for(j = 1; j <= m; ++j)
   {
    dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
    if(a[i] != b[j])
    {
     for(int p = i-1; p >= 1; --p)
     {
      for(int q = j-1; q >= 1; --q)
      {
       if(a[p] == b[j] && b[q] == a[i])
       {
        dp[i][j] = max(dp[i][j],dp[p-1][q-1]+2);
       }
      }
     }
    }
   }
  }
  printf("%d\n",dp[n][m]);
 }

 return 0;
}


优化后的代码2: times: 0MS

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
#define MAX 205

int n, m;
int a[MAX], b[MAX];
int p[MAX][MAX], q[MAX][MAX];
int dp[MAX][MAX];

inline int max(int a,int b)
{
      return a > b ? a : b;
}

int main()
{
//      freopen("input.txt","r",stdin);
      int caseNum;
      scanf("%d",&caseNum);
      while(caseNum--)
      {
            scanf("%d %d",&n,&m);
            memset(dp,0,sizeof(dp));
            memset(p,0,sizeof(p));
            memset(q,0,sizeof(q));
           
            for(int i = 1; i <= n; ++i)
                  scanf("%d",&a[i]);
            for(int j = 1; j <= m; ++j)
                  scanf("%d",&b[j]);
                 
            for(int i = 1; i <= n; ++i)
            {
                  for(int j = 1; j <= m; ++j)
                  {
                        if(a[i] == b[j-1])
                              q[i][j] = j-1;
                        else
                              q[i][j] = q[i][j-1];
                  }
            }
           
            for(int i = 1; i <= m; ++i)
            {
                  for(int j = 1; j <= n; ++j)
                  {
                        if(b[i] == a[j-1])
                              p[i][j] = j-1;
                        else
                              p[i][j] = p[i][j-1];
                  }
            }
           
            for(int i = 1; i <= n; ++i)
            {
                  for(int j = 1; j <= m; ++j)
                  {
                        dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
                        int qi = q[i][j];
                        int pj = p[j][i];
                        if(qi > 0 && pj > 0 && a[i] != b[j])
                        {
                              dp[i][j] = max(dp[i][j],dp[pj-1][qi-1]+2);
                        }
                  }
            }
            printf("%d\n",dp[n][m]);
      }
     
      return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值