POJ 1692 Crossed Matchings(DP)
|
Description
There are two rows of positive integer numbers. We can draw one line segment between any two equal numbers, with values r, if one of them is located in the first row and the other one is located in the second row. We call this line segment an r-matching segment.
The following figure shows a 3-matching and a 2-matching segment. ![]() 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
The 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
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 Sample Output 6 题意:同样数字能够连接可是必须和不同数字的连接交叉。问最大可能性 dp[i][j]表示第一行的前i个和第二行的前j个的最大可能。 |
#include<limits.h>
using namespace std;
int a[110],b[110];
int dp[110][110];
int n,m,t; int main()
{
int k1,k2;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int j=1;j<=m;j++)
scanf("%d",&b[j]);
memset(dp,0,sizeof(dp));
for(int i=2;i<=n;i++)
{
for(int j=2;j<=m;j++)
{
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);//相等时可达到的dp[i][j]的状态的最大值
if(a[i]!=b[j])
{
for(k1=i;k1>=1;k1--)
{
if(b[j]==a[k1])
break;
}
for(k2=j;k2>=1;k2--)
{
if(a[i]==b[k2])
break;
}
if(k1&&k2)
dp[i][j]=max(dp[i][j],dp[k1-1][k2-1]+2);//更新dp[i][j]
}
}
}
printf("%d\n",dp[n][m]);
}
return 0;
}
POJ 1692 Crossed Matchings(DP)的更多相关文章
- POJ 1692 Crossed Matchings dp[][] 比较有意思的dp
http://poj.org/problem?id=1692 这题看完题后就觉得我肯定不会的了,但是题解却很好理解.- - ,做题阴影吗 所以我还是需要多思考. 题目是给定两个数组,要求找出最大匹配数 ...
- 【POJ 3071】 Football(DP)
[POJ 3071] Football(DP) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4350 Accepted ...
- 【POJ】1692 Crossed Matchings
经典DP,想了很久,开始想复杂了. #include <iostream> using namespace std; #define MAXNUM 100 int mymax(int a, ...
- POJ 1260:Pearls(DP)
http://poj.org/problem?id=1260 Pearls Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8 ...
- POJ 2192 :Zipper(DP)
http://poj.org/problem?id=2192 Zipper Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1 ...
- POJ 3254 Corn Fields(DP + 状态压缩)
题目链接:http://poj.org/problem?id=3254 题目大意:Farmer John 放牧cow,有些草地上的草是不能吃的,用0表示,然后规定两头牛不能相邻放牧.问你有多少种放牧方 ...
- poj - 1191 - 棋盘切割(dp)
题意:将一个8*8的棋盘(每一个单元正方形有个分值)沿直线(竖或横)割掉一块,留下一块,对留下的这块继续这样操作,总共进行n - 1次,得到n块(1 < n < 15)矩形,每一个矩形的分 ...
- POJ 1160 Post Office(DP+经典预处理)
题目链接:http://poj.org/problem?id=1160 题目大意:在v个村庄中建立p个邮局,求所有村庄到它最近的邮局的距离和,村庄在一条直线上,邮局建在村庄上. 解题思路:设dp[i] ...
- POJ 1191 棋盘分割(DP)
题目链接 题意 : 中文题不详述. 思路 : 黑书上116页讲的很详细.不过你需要在之前预处理一下面积,那样的话之后列式子比较方便一些. 先把均方差那个公式变形, 另X表示x的平均值,两边平方得 平均 ...
随机推荐
- .NET中的IO操作之文件流(一)
读操作 //1.创建文件流 FileStream fsRead =new FileStream("1.txt",FileMode.Open); //2.创建缓冲区,正常情况下,是不 ...
- Symfony命令行
Available commands: help 显示命令的帮助信息 list ...
- Java 多线程 socket 取款例子 runnable callable
socket部分参考 http://blog.csdn.net/kongxx/article/details/7259465 取款部分参考 http://blog.csdn.net/dayday198 ...
- 怪兽z主机标准版视频测试。
我的淘宝店很早就开张了,但是一直没有好好打理,这次给大家带来的是本店所售一款主机的视频测试. CPU:AMD -A10 6700 主板:映泰Hi-Fi A88S3E 内存条:正品金士顿骇客游戏神条 机 ...
- Delphi 的动态数组
传统的Pascal 语言其数组大小是预先确定的,当你用数组结构声明数据类型时,你必须指定数组元素的个数.专业程序员也许知道些许动态数组的实现技术,一般是采用指针,用手工分配并释放所需的内存. Delp ...
- PHP查看在线服务器与本地服务器支持函数差别
在本地开发好的程序.代码,当传到服务器后,经常会出现罢工的事情.这时候就要考虑开发环境和在线的环境是否完全一致了.这里所说的一致性,包括了容器的配置.php的配置等等.下面所要解决的只是其中一项,你的 ...
- Android开发小记
一,下载解压adt-bundle,直接可以用来开发了二,新建android项目时不勾选创建activity,来看看如何手动创建activity1,在空项目添加class文件,选择超类为activity ...
- Winter(bfs&&dfs)
1084 - Winter PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Winter is ...
- oracle 中的select ...connect by prior ...start with 及(+)的用法
1.select ...connect by prior ...start with的用法: select ... from <tablename> where <condition ...
- PHP开发中常见的安全问题详解和解决方法(如Sql注入、CSRF、Xss、CC等
页面导航: 首页 → 网络编程 → PHP编程 → php技巧 → 正文内容 PHP安全 PHP开发中常见的安全问题详解和解决方法(如Sql注入.CSRF.Xss.CC等) 作者: 字体:[增加 减小 ...

