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匹马, 用一张马车票从一个城市到另一个城市所用的时间为这两个 ...
随机推荐
- debian old version cd and distribution archives
1 debian old version cd/dvd 官网的old version image,下载速度很慢 http://cdimage.debian.org/cdimage 下面这个靠谱,是镜像 ...
- PM12条
PM首先是用户 站在用户角度看待问题 用户体验是一个完整的过程 追求效果,不做没用的东西 发现需求,而不是创造需求 决定不做什么,往往比决定做什么更重要 用户是很难被教育的,要迎合用户,而不是改变用户 ...
- manacher求最长回文子串算法模板
#include <iostream> #include <cstring> #include <cstdlib> #include <stdio.h> ...
- PHP加密方式。 base!base!base!
PHP中的加密方式有如下几种 1. MD5加密 string md5 ( string $str [, bool $raw_output = false ] ) 参数 str -- 原始字符串. ...
- WPF 之Converter
WPF 之Converter Leo 在我们做项目的时候,经常会遇见这样的事情: 在数据中我们定义的是true,false 而在现实的时候则可能要求男,女 我们还得能定义成了0,1,2,3,4,5, ...
- oracle:对Index重建、分析
对index进行分析,index_stats 表很有用.下面例子就结合index相关操作及 index_stats 的使用,对index进行分析. SQL> select count(*) fr ...
- zoj 3204 Connect them(最小生成树)
题意:裸最小生成树,主要是要按照字典序. 思路:模板 prim: #include<iostream> #include<stdio.h> #include<string ...
- SpringBoot启动的时候不去校验数据库连接配置是否正确
spring boot在启动的时候只会检查你是否配置了数据库连接, 而不会检测配置的是否正确 这样会出现的问题是: 只有在你使用数据库的时候才知道配置出错, 我们希望是在程序启动的时候就进行检查, 如 ...
- A. Hulk
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- Linux系统安装完的调整和安全
精简开机系统自启动 •五个企业环境中开机自启动的服务; sshd:远程连接linux服务器必须开启 rsyslog:日志相关软件 network:网络服务 crond:系统和用户配置的计划任务周期性进 ...