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 ...
随机推荐
- jquery dblclick()方法 语法
jquery dblclick()方法 语法 作用:当双击元素时,会发生 dblclick 事件.当鼠标指针停留在元素上方,然后按下并松开鼠标左键时,就会发生一次 click.在很短的时间内发生两次 ...
- 一些简单题(2)(Source : NOIP历年试题+杂题)
P3084 [USACO13OPEN]照片Photo 给出$m$个区间$[l_i,r_i]$覆盖$S=[1,n]$,试确定最大特殊点的数使得这每一个区间覆盖且仅覆盖一个特殊点. 如果无解,输出$-1$ ...
- Luogu P4707 重返现世 (拓展Min-Max容斥、DP)
题目链接 https://www.luogu.org/problem/P4707 题解 最近被神仙题八连爆了-- 首先Min-Max容斥肯定都能想到,问题是这题要用一个扩展版的--Kth Min-Ma ...
- JavaWeb_Servlet生命周期
菜鸟教程 传送门 Servlet生命周期 package com.Gary.servlet; import java.io.IOException; import javax.servlet.Serv ...
- Apicloud_(项目)网上书城01_前端搭建
[本文皆在记录自己开发Apicloud项目过程,不具备教学水平性文章] 参考书籍<30天App开发从0到1> Apicloud_(项目)网上书城01_前端页面开发 传送门 Apicloud ...
- war包部署到tomcat
1.maven web app打包成app.war.打包命令:mvn clean package Dmaven.test.skip=true war 是什么?里面有什么东西?a.web.app所有必 ...
- @transient 注解 和 transient变量的作用
@transient 和 transient是两码事 1.@transient的作用 @transient是hibernate和Morphia中的注解,hibernate都熟悉,Morphia是通过同 ...
- 安卓项目集成objectbox-java数据库框架教程(非关系型)
objectbox数据库是greenrobot团队开发的全新非关系型数据库框架,该团队还开发过greenDao,EventBus等热门框架,objectbox可能是第一次听说,但是greenDao,E ...
- Spring之AOP配置
特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...
- [论文理解] CBAM: Convolutional Block Attention Module
CBAM: Convolutional Block Attention Module 简介 本文利用attention机制,使得针对网络有了更好的特征表示,这种结构通过支路学习到通道间关系的权重和像素 ...