POJ 2955 Brackets(括号匹配一)
题目链接:http://poj.org/problem?id=2955
题目大意:给你一串字符串,求最大的括号匹配数。
解题思路:
设dp[i][j]是[i,j]的最大括号匹配对数。
则得到状态转移方程:
if(str[i]=='('&&str[j]==')'||(str[i]=='['&&str[j]==']')){
dp[i][j]=dp[i+1][j-1]+1;
}
dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]) ,(i<=k<j)
代码:
#include<cstdio>
#include<cmath>
#include<cctype>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<string>
#define lc(a) (a<<1)
#define rc(a) (a<<1|1)
#define MID(a,b) ((a+b)>>1)
#define fin(name) freopen(name,"r",stdin)
#define fout(name) freopen(name,"w",stdout)
#define clr(arr,val) memset(arr,val,sizeof(arr))
#define _for(i,start,end) for(int i=start;i<=end;i++)
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef long long LL;
const int N=5e2+;
const int INF=0x3f3f3f3f;
const double eps=1e-; int dp[N][N];
char str[N]; int main(){
while(~scanf("%s",str)&&strcmp(str,"end")){
int n=strlen(str);
memset(dp,,sizeof(dp));
for(int len=;len<n;len++){
for(int i=;i+len<n;i++){
int j=i+len;
if(str[i]=='('&&str[j]==')'||(str[i]=='['&&str[j]==']')){
dp[i][j]=dp[i+][j-]+;
}
for(int k=i;k<j;k++){
dp[i][j]=max(dp[i][j],dp[i][k]+dp[k+][j]);
}
}
}
printf("%d\n",dp[][n-]*);
}
return ;
}
POJ 2955 Brackets(括号匹配一)的更多相关文章
- poj 2955 Brackets 括号匹配 区间dp
题意:最多有多少括号匹配 思路:区间dp,模板dp,区间合并. 对于a[j]来说: 刚開始的时候,转移方程为dp[i][j]=max(dp[i][j-1],dp[i][k-1]+dp[k][j-1]+ ...
- POJ - 2955 Brackets括号匹配(区间dp)
Brackets We give the following inductive definition of a “regular brackets” sequence: the empty sequ ...
- poj 2955 Brackets
题目链接:http://poj.org/problem?id=2955 思路:括号匹配问题,求出所给序列中最长的可以匹配的长度(中间可以存在不匹配的)例如[(])]有[()]符合条件,长度为4 dp[ ...
- poj 2955 Brackets (区间dp 括号匹配)
Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...
- POJ-2955 Brackets(括号匹配问题)
题目链接:http://poj.org/problem?id=2955 这题要求求出一段括号序列的最大括号匹配数量 规则如下: the empty sequence is a regular brac ...
- Poj 2955 brackets(区间dp)
Brackets Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7795 Accepted: 4136 Descript ...
- POJ 2955 Brackets(区间DP)题解
题意:问最多有几个括号匹配 思路:用dp[i][j]表示i到j最多匹配,若i和j构成匹配,那么dp[i][j] = dp[i + 1][j - 1] + 2,剩下情况dp[i][j] = max(dp ...
- Sereja and Brackets(括号匹配)
Description Sereja has a bracket sequence s1, s2, ..., sn, or, in other words, a string s of length ...
- poj 2955 Brackets dp简单题
//poj 2955 //sep9 #include <iostream> using namespace std; char s[128]; int dp[128][128]; int ...
随机推荐
- C/C++:copy control (拷贝控制)
前言:当定义一个类的时候,我们显示或者隐式地指定在此类型的对象拷贝,移动,赋值,销毁时做些什么,一个类通过定义五种特殊的成员函数来控制这些操作,包括拷贝构造函数,拷贝赋值运算符,移动构造函数,移动赋值 ...
- 语法:c++对关于空指针0/NULL/nullptr三者的演变
来源: https://blog.csdn.net/u010558281/article/details/77793644 字面意义上的解释: 0:整型常量 NULL:预处理符号 nullptr:空指 ...
- 1044 Shopping in Mars
Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diam ...
- bzoj 2002
2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 12203 Solved: 6162[Subm ...
- ORM choice字段 如何在页面上显示值
核心:obj.get_字段名_display 1.定义module 数据结构: class msg(models.Model): choice = ( (1, '技术部'), (2, '行政'), ( ...
- supervisor自启动
supervisor自启动 其实自启动,也就是在主机开启的时候,执行了sudo supervisord -c /etc/supervisord.conf: 创建/usr/lib/systemd/sys ...
- http协议POST请求头content-type主要的四种取值
介绍: 在此之前对content-type理解很肤浅,因此必须记录下来现在的理解,以便回顾 Content-Type,从名字上可以理解为内容类型,但在互联网上专业术语叫“媒体类型”,即MediaTyp ...
- uploadify IE11 不兼容问题(不显示图片)
1.进入uploadify官网demo : http://www.uploadify.com/demos/ 2. 显示 (确认flash为最新版本) 3.更换其它浏览器一切正常 4.原因:I ...
- Java入门系列(四)内部类
为什么需要内部类? 真正的原因是这样的,java中的内部类和接口加在一起,可以的解决常被C++程序员抱怨java中存在的一个问题没有多继承.实际上,C++的多继承设计起来很复杂,而java通过内部类加 ...
- ArraySizeHelper解析
[ArraySizeHelper解析] 以下代码用于获取一个数组的元素个数,例如 int table[100],以下宏返回100. template <typename T, size_t N& ...