POJ 1692 Crossed Matchings dp[][] 比较有意思的dp
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的更多相关文章
- POJ 1692 Crossed Matchings(DP)
Description There are two rows of positive integer numbers. We can draw one line segment between any ...
- 【POJ】1692 Crossed Matchings
经典DP,想了很久,开始想复杂了. #include <iostream> using namespace std; #define MAXNUM 100 int mymax(int a, ...
- poj 1692(动态规划)
Crossed Matchings Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2711 Accepted: 1759 ...
- POJ 3286 How many 0's(数位DP模板)
题目链接:http://poj.org/problem?id=3286 题目大意: 输入n,m,求[n,m]的所有数字中,0出现的总数是多少,前导零不算. 解题思路: 模板题,设dp[pos][num ...
- POJ 1946 Cow Cycling(抽象背包, 多阶段DP)
Description The cow bicycling team consists of N (1 <= N <= 20) cyclists. They wish to determi ...
- POJ 1949 Chores (很难想到的dp)
传送门: http://poj.org/problem?id=1949 Chores Time Limit: 3000MS Memory Limit: 30000K Total Submissio ...
- 【POJ 3140】 Contestants Division(树型dp)
id=3140">[POJ 3140] Contestants Division(树型dp) Time Limit: 2000MS Memory Limit: 65536K Tot ...
- [ACM] POJ 2151 Check the difficulty of problems (概率+DP)
Check the difficulty of problems Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4748 ...
- POJ 2686 Traveling by Stagecoach(状压DP)
[题目链接] http://poj.org/problem?id=2686 [题目大意] 给出一张无向图,你有n张马车票每张车票可以租用ti匹马, 用一张马车票从一个城市到另一个城市所用的时间为这两个 ...
随机推荐
- noip2016前的话[漫谈]
今天是11月15日,离noip2016还剩三天: 今年我也是高二了,回首一下去年的时光,真的仿佛仍在昨天,我甚至现在还清楚的记得,当年那次我们做的每一件事: 星期五,回去与室友告别,得到了祝愿,乘公交 ...
- chmod更改文件的权限
#include "apue.h" int main(int argc,char *argv[]) { struct stat stabuf; ) err_sys("st ...
- POJ 3750 小孩报数问题 (线性表思想 约瑟夫问题 数组模拟运算的 没用循环链表,控制好下标的指向就很容易了)
小孩报数问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10423 Accepted: 4824 Descripti ...
- 基于Vuejs的搜索匹配功能
最近一直在看vue,查了很多资料,看了很多文档和博客,大概半知半解了,然后利用所理解的知识写了一个简单的搜索匹配功能. 大概长这个样子: <!DOCTYPE html> <htm ...
- ssm使用velocity模板语言
1.在pom.xml里添加velocity模板语言支持的依赖 <!-- velocity模板语言支持包 --> <dependency> <groupId>org. ...
- hdu 2680 Choose the best route 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2680 题目意思:实质就是给定一个多源点到单一终点的最短路. 卑鄙题---有向图.初始化map时 千万不 ...
- Docker安装 人生第一次
Ubuntu 系列安装 Docker 通过系统自带包安装 Ubuntu 14.04 版本系统中已经自带了 Docker 包,可以直接安装. $ sudo apt-get update $ sudo a ...
- golang defer使用——资源关闭时候多用
defer Go语言中有种不错的设计,即延迟(defer)语句,你可以在函数中添加多个defer语句.当函数执行到最后时,这些defer语句会按照逆序执行,最后该函数返回.特别是当你在进行一些打开资源 ...
- 「LuoguP3865」 【模板】ST表 (线段树
题目背景 这是一道ST表经典题——静态区间最大值 请注意最大数据时限只有0.8s,数据强度不低,请务必保证你的每次查询复杂度为 O(1) 题目描述 给定一个长度为 N 的数列,和 M 次询问,求出每一 ...
- cocos2dx-js 初探 整体流程helloworld.html分析
我们下载的是cocos2dx-js的精简版本,主要是为了分析简单明了,能更清楚的看到架构流程.下载地址:http://cocos2d-x.org/filecenter/jsbuilder/下载轻量版. ...