poj 1692(动态规划)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 2711 | Accepted: 1759 |
Description

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.

Write a program to compute the maximum number of matching segments for the input data. Note that this number is always even.
Input
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
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
Source
#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;
const int N = ; int dp[N][N]; ///dp[i][j]表示第1行前i个字符和第二行前j个字符的最大匹配
int main()
{
int tcase;
int a[N],b[N];
scanf("%d",&tcase);
while(tcase--){
int n1,n2;
scanf("%d%d",&n1,&n2);
for(int i=;i<=n1;i++) {
scanf("%d",&a[i]);
}
for(int i=;i<=n2;i++){
scanf("%d",&b[i]);
}
memset(dp,,sizeof(dp));
for(int i=;i<=n1;i++){
for(int j=;j<=n2;j++){
dp[i][j] = max(dp[i-][j],dp[i][j-]);
if(a[i]!=b[j]){
int k1,k2;
for(k1 = i-;k1>;k1--){
if(a[k1]==b[j]) break;
}
for(k2=j-;k2>;k2--){
if(b[k2]==a[i]) break;
}
if(k1!=&&k2!=){
dp[i][j] = max(dp[i][j],dp[k1-][k2-]+); ///在 dp[k1-1][k2-1]之后又产生了两组新的匹配
}
}
}
}
printf("%d\n",dp[n1][n2]);
}
return ;
}
poj 1692(动态规划)的更多相关文章
- POJ 1692 Crossed Matchings dp[][] 比较有意思的dp
http://poj.org/problem?id=1692 这题看完题后就觉得我肯定不会的了,但是题解却很好理解.- - ,做题阴影吗 所以我还是需要多思考. 题目是给定两个数组,要求找出最大匹配数 ...
- nyoj 17-单调递增最长子序列 && poj 2533(动态规划,演算法)
17-单调递增最长子序列 内存限制:64MB 时间限制:3000ms Special Judge: No accepted:21 submit:49 题目描述: 求一个字符串的最长递增子序列的长度 如 ...
- poj 3034 动态规划
思路:这是一道坑爹的动态规划,思路很容易想到,就是细节. 用dp[t][i][j],表示在第t时间,锤子停在(i,j)位置能获得的最大数量.那么只要找到一个点转移到(i,j)收益最大即可. #incl ...
- poj 2498 动态规划
思路:简单动态规划 #include<map> #include<set> #include<cmath> #include<queue> #inclu ...
- poj 2287 动态规划
用贪心简单证明之后就是一个从两头取的动态规划 #include <iostream> #include <cstring> #include <cstdio> #i ...
- POJ 2533 动态规划入门 (LIS)
Longest Ordered Subsequence Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 42914 Accepte ...
- poj 1821 动态规划
思路:每次枚举每个工人的右边界j,维护最优的左边界k.那么dp[j]=max(dp[j],dp[k]+(j-k)*w[i].p): 对于每个工人的初值k=w[i].s-1; 令x=j-w[i].l,如 ...
- poj 1390 动态规划
思路: 黑书的例题 #include<iostream> #include<cstring> #include<algorithm> #include<cma ...
- poj 1695 动态规划
思路:和黑书上的跳舞机类似 #include<map> #include<set> #include<cmath> #include<queue> #i ...
随机推荐
- MySQL基础原创笔记(二)
表索引关键字:PRI primary key 表示主键,唯一 写法: id bigint(20) unsigned primary key not null ,uni UNIQUE 表示唯一 id b ...
- "HK"日常之冻结术
在那遥远的MSDN上,有那么一只被隐藏的函数,它掌管着Windows内核威力不容小觑~ 本教程仅作为学习研究,禁止其他用途! 富强.民主.文明.和谐, 自由.平等.公正.法治, 爱国.敬业.诚信.友善 ...
- javascript forEach无法break,使用every代替
every的入口参数是一个返回bool值的函数,在需要break的地方return false,其他均return true,即可达到和break相同的效果 function find(arr2, e ...
- SQL Server 2008设置主键为自增
环境:SQL Server 2008 问题:设置主键,将主键设为自增. 解决:点击table->选中表->design->选中需要设置主键的字段,单击右键"设置主键&quo ...
- 复习java数据库操作的总结
以前学习java数据库操作,学得那叫糊里糊涂,各种JDBC常用的类和接口根本是傻傻分不清啥是干嘛的.只是套着用用吧. 不过这次好歹清楚些了,呜呜,学习有阶段性,多次重复才有好效果,多么痛的领悟. 工程 ...
- AndroidManifest Ambiguity方案原理及代码
1简述 前段时间在bluebox的一份android安全pdf中看到一个AndroidManifest Ambiguity方案.该方案基于android系统解析AXML的一个特点:android在解析 ...
- linux查找进程id端口占用和杀死进程
linux 查找进程id端口占用和杀死进程 ps 命令用于查看当前正在运行的进程 辅助上grep 用于搜索匹配ps -ef | grep java ps ax : 显示当前系统进程的列表 ps aux ...
- [洛谷P1707] 刷题比赛
洛谷题目连接:刷题比赛 题目背景 nodgd是一个喜欢写程序的同学,前不久洛谷OJ横空出世,nodgd同学当然第一时间来到洛谷OJ刷题.于是发生了一系列有趣的事情,他就打算用这些事情来出题恶心大家-- ...
- Commonjs,AMD,CMD和UMD的差异
CommonJS 一种服务器端模块化的规范,Nodejs实现了这种规范,所以就说Nodejs支持CommonJS. CommonJS分为三部分: require 模块加载 exports 模块导出 m ...
- nodejs入门教程之http的get和request简介及应用
nodejs入门教程之http的get和request简介及应用 前言 上一篇文章,我介绍了nodejs的几个常用的模块及简单的案例,今天我们再来重点看一下nodejs的http模块,关于http模块 ...