http://poj.org/problem?id=1692

这题看完题后就觉得我肯定不会的了,但是题解却很好理解。- - ,做题阴影吗

所以我还是需要多思考。

题目是给定两个数组,要求找出最大匹配数量。

匹配规则是:

a[i] ==b[j],而且需要产生交叉,而且没对数只能匹配一次。

一开始的时候我被交叉吓到了,怎么判断交叉啊?

分析:

对于a[]的第i个数,b数组的第j个数,假设a[i] != b[j] 如果它能产生匹配,那么就需要,

对于a[i]这个数字,在b[]的前j - 1个找到和他数字相等的,

对于b[j]这个数字,在a[]的前i - 1个数中找到一个数字,和b[j]相等,

这个时候,他们不仅能匹配,而且还是合法了,就是交叉了。所以在这个状态转移过来就好。

所以设dp[i][j]表示a[]的前i个数,b[]的前j个数。能匹配的最大数量。

dp[i][j] = max(dp[i][j], dp[posa - 1][posb - 1] + 2);

当然,第i个数和第j个数也可以不用来匹配,但是要把答案传递下去,所以开始的时候。

dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset> const int maxn = 1e2 + ;
int dp[maxn][maxn];
int a[maxn], b[maxn];
int toFindPos(int nowPos, int val, int which) {
if (which == ) {
while (nowPos--) {
if (nowPos == ) return inf;
if (a[nowPos] == val) return nowPos;
}
} else {
while(nowPos--) {
if (nowPos == ) return inf;
if (b[nowPos] == val) return nowPos;
}
}
assert(false);
}
void work() {
int lena, lenb;
cin >> lena >> lenb;
for (int i = ; i <= lena; ++i) cin >> a[i];
for (int i = ; i <= lenb; ++i) cin >> b[i];
memset(dp, , sizeof dp);
for (int i = ; i <= lena; ++i) {
for (int j = ; j <= lenb; ++j) {
dp[i][j] = max(dp[i - ][j], dp[i][j - ]);
if (a[i] == b[j]) continue;
int posA = toFindPos(i, b[j], );
int posB = toFindPos(j, a[i], );
if (posA == inf || posB == inf) continue;
dp[i][j] = max(dp[i][j], dp[posA - ][posB - ] + );
}
}
cout << dp[lena][lenb] << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
int t;
cin >> t;
while (t--) work();
return ;
}

POJ 1692 Crossed Matchings dp[][] 比较有意思的dp的更多相关文章

  1. POJ 1692 Crossed Matchings(DP)

    Description There are two rows of positive integer numbers. We can draw one line segment between any ...

  2. 【POJ】1692 Crossed Matchings

    经典DP,想了很久,开始想复杂了. #include <iostream> using namespace std; #define MAXNUM 100 int mymax(int a, ...

  3. poj 1692(动态规划)

    Crossed Matchings Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2711   Accepted: 1759 ...

  4. POJ 3286 How many 0's(数位DP模板)

    题目链接:http://poj.org/problem?id=3286 题目大意: 输入n,m,求[n,m]的所有数字中,0出现的总数是多少,前导零不算. 解题思路: 模板题,设dp[pos][num ...

  5. POJ 1946 Cow Cycling(抽象背包, 多阶段DP)

    Description The cow bicycling team consists of N (1 <= N <= 20) cyclists. They wish to determi ...

  6. POJ 1949 Chores (很难想到的dp)

    传送门: http://poj.org/problem?id=1949 Chores Time Limit: 3000MS   Memory Limit: 30000K Total Submissio ...

  7. 【POJ 3140】 Contestants Division(树型dp)

    id=3140">[POJ 3140] Contestants Division(树型dp) Time Limit: 2000MS   Memory Limit: 65536K Tot ...

  8. [ACM] POJ 2151 Check the difficulty of problems (概率+DP)

    Check the difficulty of problems Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4748   ...

  9. POJ 2686 Traveling by Stagecoach(状压DP)

    [题目链接] http://poj.org/problem?id=2686 [题目大意] 给出一张无向图,你有n张马车票每张车票可以租用ti匹马, 用一张马车票从一个城市到另一个城市所用的时间为这两个 ...

随机推荐

  1. Codeforces 8VC Venture Cup 2016 - Elimination Round F. Group Projects 差分DP*****

    F. Group Projects   There are n students in a class working on group projects. The students will div ...

  2. SQL常见问题及解决备忘

    1.mysql中:you cant't specify tartget table for update in from clause 错误 含义:在同一语句中update或delete某张表的时候, ...

  3. ivy

    ivy https://ant.apache.org/ivy/ Apache Ivy™ is a popular dependency manager focusing on flexibility ...

  4. iframe 框架中 父子界面的JS调用

    子界面调用父界面 window.parent.hello(); 父界面调用子界面 window.frmaes[i].hello();

  5. BigDecimal快速使用

    double类型最多支持16位有效数字,且最大值只支持10^308次方,大一点的数字会变为科学计数法,小数精度不够等有一系列不方便的问题: 引进BigDecimal解决此类麻烦,弊端,BigDecim ...

  6. HDU4283 You Are the One —— 区间DP

    题目链接:https://vjudge.net/problem/HDU-4283 You Are the One Time Limit: 2000/1000 MS (Java/Others)    M ...

  7. 如何反编译silverlight

     @years(945060991)  15:10:28问一下  如何反编译silverlight观,一世沧桑如画♥(752816388)  15:10:46解压就行@years(945060991) ...

  8. 【Maven】pom.xml(1)

    在pom.xml加入: <build> <finalName>oauth2</finalName> <resources> <resource&g ...

  9. SPOJ:Eagle and Dogs(求树上每个点最远可以走到哪里---树的直径||DP)

    Eagle (AKA Mohamed Ahmed) lives in a city consists of n intersections connected by n-1 roads, in a w ...

  10. 哈希表的C实现(一)

    哈希表(Hash table,也叫散列表),是根据关键码值(Key value)而直接进行访问的数据结构.也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度.具体的介绍网上有很详 ...