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 ...
随机推荐
- jq的each理解
1种 通过each遍历li 可以获得所有li的内容 <!-- 1种 --> <ul class="one"> <li>11a</li> ...
- 如何设置Eclipse使用JDK
1.打开Eclipse,选择Windows->Preferences,如图所示: 2.配置本地安装的jdk,如图所示: 注意:首先要先安装JDK. 木头大哥所发的文章均基于自身实践, ...
- Unresolved import *** (models) error on Eclipse
Eclipse version: Oxygen.2 Release (4.7.2) Python version: 3.6 问题:系统提示:from django.db import models 语 ...
- Sql Server 2008 R2 数据库脚本导出方法
经常忘记怎么搞,截几张图记录一下. 1 选中要导出的数据库,右键—>任务—>生成脚本 2 3 4 查看保存的脚本
- Leetcode 001. 两数之和(扩展)
1.题目要求 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 2.解法一:暴力法(for*for,O(n*n)) ...
- HDU1522 稳定婚姻匹配 模板
Marriage is Stable Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- Java并发多线程 - 并发工具类JUC
安全共享对象策略 1.线程限制 : 一个被线程限制的对象,由线程独占,并且只能被占有它的线程修改 2.共享只读 : 一个共享只读的对象,在没有额外同步的情况下,可以被多个线程并发访问, 但是任何线程都 ...
- HDU 4609 FFT模板
http://acm.hdu.edu.cn/showproblem.php?pid=4609 题意:给你n个数,问任意取三边能够,构成三角形的概率为多少. 思路:使用FFT对所有长度的个数进行卷积(\ ...
- PAT (Top Level)1002. Business DP/背包
As the manager of your company, you have to carefully consider, for each project, the time taken to ...
- loj6102 「2017 山东二轮集训 Day1」第三题
传送门:https://loj.ac/problem/6102 [题解] 贴一份zyz在知乎的回答吧 https://www.zhihu.com/question/61218881 其实是经典问题 # ...