POJ 2995 Brackets 区间DP
POJ 2995 Brackets 区间DP
题意
大意:给你一个字符串,询问这个字符串满足要求的有多少,()和[]都是一个匹配。需要注意的是这里的匹配规则。
解题思路
区间DP,开始自己没想到是区间DP,以为就是用栈进行模拟呢,可是发现就是不大对,后来想到是不是使用DP,但是开始的时候自己没有推出递推关系,后来实在想不出来看的题解,才知道是区间DP,仔细一想确实是啊。
下面就是状态转移方程:
\]
当初知道了转移方程,就自己写代码,可是就是不对,下面有两个代码,一个是错误的,一个是正确的,两个对比看一看原因。
代码实现
//这个是正确的
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1e3+7;
char str[maxn];
int dp[maxn][maxn];
int main()
{
while(scanf("%s", &str))
{
if(strcmp("end", str)==0)
break;
int n=strlen(str);
memset(dp, 0, sizeof(dp));
//下面书写的格式很重要,先算长度为1的区间,然后再算区间为2的区间,以此类推
for(int len=1; len<=n; len++)
{
for(int L=0; L+len<n; L++)
{
int R=L+len;
if((str[L]=='(' && str[R]==')') || (str[L]=='[' && str[R]==']'))
{
dp[L][R]=dp[L+1][R-1]+2;
}
for(int k=L; k<R; k++)
{
dp[L][R]=max(dp[L][R], dp[L][k]+dp[k+1][R]);
}
}
}
printf("%d\n", dp[0][n-1]);
}
return 0;
}
//这个是错误的代码,下面分析主要原因,连样例都过不了
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
using namespace std;
stack<char> st;
const int maxn=1e4+7;
char str[maxn];
int dp[maxn][maxn];
int main()
{
while(scanf("%s", str))
{
if(strcmp("end", str)==0)
break;
int n=strlen(str);
memset(dp, 0, sizeof(dp));
//下面的代码其实是有点问题的,应该是先算长度全为1的区间段,然后再是长度为2的,以此类推
//为什么要这这样呢,因为下面的max函数中第二项是一个重要的部分
for(int L=0; L<len; L++)
{
for(int R=i+1; R<len; R++)
{
dp[L][R]=dp[L+1][R-1];
if(str[L]=='(' && str[R]==')' || str[L]=='[' && str[R]==']')
{
dp[L][R]+=2;
}
for(int k=L; k<R; k++)
{
//下面的后两项之和应该在计算dp[L][R]之前就应该计算了,但是这里可能没有。
//所以区间DP的书写格式还是有点套路的。
dp[L][R]=max(dp[L][R], dp[L][k]+dp[k+1][R]);
}
}
}
printf("%d\n", dp[0][len-1]);
}
return 0;
}
POJ 2995 Brackets 区间DP的更多相关文章
- HOJ 1936&POJ 2955 Brackets(区间DP)
Brackets My Tags (Edit) Source : Stanford ACM Programming Contest 2004 Time limit : 1 sec Memory lim ...
- poj 2955 Brackets (区间dp基础题)
We give the following inductive definition of a “regular brackets” sequence: the empty sequence is a ...
- poj 2955"Brackets"(区间DP)
传送门 https://www.cnblogs.com/violet-acmer/p/9852294.html 题意: 给你一个只由 '(' , ')' , '[' , ']' 组成的字符串s[ ], ...
- poj 2955 Brackets (区间dp 括号匹配)
Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...
- POJ 2955 Brackets 区间DP 入门
dp[i][j]代表i->j区间内最多的合法括号数 if(s[i]=='('&&s[j]==')'||s[i]=='['&&s[j]==']') dp[i][j] ...
- POJ 2955 Brackets(区间DP)
题目链接 #include <iostream> #include <cstdio> #include <cstring> #include <vector& ...
- POJ 2955 Brackets 区间DP 最大括号匹配
http://blog.csdn.net/libin56842/article/details/9673239 http://www.cnblogs.com/ACMan/archive/2012/08 ...
- Codeforces 508E Arthur and Brackets 区间dp
Arthur and Brackets 区间dp, dp[ i ][ j ]表示第 i 个括号到第 j 个括号之间的所有括号能不能形成一个合法方案. 然后dp就完事了. #include<bit ...
- poj2955 Brackets (区间dp)
题目链接:http://poj.org/problem?id=2955 题意:给定字符串 求括号匹配最多时的子串长度. 区间dp,状态转移方程: dp[i][j]=max ( dp[i][j] , 2 ...
随机推荐
- CSS currentColor 变量
㈠currentColor定义及理解 来自MDN的解释:currentColor代表了当前元素被应用上的color颜色值. 使用它可以将当前这个颜色值应用到其他属性上,或者嵌套元素的其他属性上. 你这 ...
- nginx命令和配置
centos 6.8安装的nginx 1.12.2 1.nginx常用的命令 使用nginx命令前,进入到/usr/local/nginx/sbin/目录 1)查看nginx版本 进入到/usr/lo ...
- (转)window.parent和window.opener区别
下面一段代码是关于window.parent和window.opener区别 来讲的,我们如果要用到iframe的值传到另一框架就要用到window.opener.document.getElemen ...
- eclipse切换工作空间
- hdu_1059(多重背包)
多重背包的讲解: 多重背包问题https://blog.csdn.net/yandaoqiusheng/article/details/84782655 ; i <= n; i++) { int ...
- AtCoder AGC022C Remainder Game (图论)
题目链接 https://atcoder.jp/contests/agc022/tasks/agc022_c 题解 大水题一道 就他给的这个代价,猜都能猜到每个数只能用一次 仔细想想,我们肯定是按顺序 ...
- You Are Given a Decimal String...
B. You Are Given a Decimal String... 这个题需要求出从某一个尾数 n 变为 m 所需要的 x 和 y 的最小个数(i+j) 那么就需要预处理出一个数组来存放这个值. ...
- HTML和CSS 入门系列(一):超链接、选择器、颜色、盒模式、DIV布局、图片
一.超链接 二.CSS选择器 CSS的全称叫做: Cascading Style Sheets 级联样式表的缩写. 2.1 类型选择器 2.2 派生选择器 2.3 伪类选择器 <style &g ...
- JavaScript_AMD规范
JavaScript_AMD规范 一.总结 一句话总结: Asynchronous Module Definition:AMD是"Asynchronous Module Definition ...
- 前端学习框架之layui
学习地址:https://www.layui.com/demo/laytpl.html