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. org.gradle.api.publication.maven.internal.DefaultMavenFactory错误

    Error:Unable to load class 'org.gradle.api.publication.maven.internal.DefaultMavenFactory'. Possible ...

  2. /dev下添加设备节点的方法步骤(通过device_create)

    将自己开发的内核代码加入到Linux内核中,需要3个步骤: 1.确定把自己开发代码放入到内核合适的位置 将demo_chardev.c文件拷贝到.../drivers/char/目录下. demo_c ...

  3. 为了cider,尝试emacs的坑

    https://github.com/clojure-emacs/cider http://clojure-doc.org/articles/tutorials/emacs.html emacs通过b ...

  4. mmwave

    毫米波(mmWave) 致力于支持5G应用创新开发,集成在BEEcube BEE7基带平台上的赛灵思256QAM毫米波调制解调器IP为宽带回程原型设计提供完整的开箱即用型解决方案 赛灵思公司 (NAS ...

  5. Linux 高精度定时器hrtimer 使用示例【转】

    本文转载自:http://blog.csdn.net/dean_gdp/article/details/25481225 hrtimer的基本操作 Linux的传统定时器通过时间轮算法实现(timer ...

  6. 完美解决pip install scrapy,安装Scrapy错误:Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools

    1,在Python3.6 安装Scrapy 出现以下报错 2,错误分析 红色报的错误指向的是Twisted 1,Twisted 没安装上 2,Twisted 没安装成功 3,Twisted 版本与Py ...

  7. codeforces 441B. Valera and Fruits 解题报告

    题目链接:http://codeforces.com/problemset/problem/441/B 题目意思:有 n 棵fruit trees,每课水果树有两个参数描述:水果成熟的时间和这棵树上水 ...

  8. Oracle:impdb导入

    最近有现场给我一份用expdp导出dmp文件,我用imp导入时,报错.因为导出dmp的数据库是11g,导入的数据库也是11g, 但客户端安装的是10g,不能用imp导入:所以只能试着用impdp导入: ...

  9. HDU - 1269 迷宫城堡(有向图的强连通分量)

    d.看一个图是不是强连通图 s.求出强连通分量,看看有没有一个强连通分量包含所有点. c.Tarjan /* Tarjan算法 复杂度O(N+M) */ #include<iostream> ...

  10. sublime text 3 安装vue 语法插件

    1.下载https://github.com/vuejs/vue-syntax-highlight,点击这里也可以下载压缩包 2.解压到C:\Users\***\AppData\Roaming\Sub ...