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 ...
随机推荐
- laravel 中的 toSql 获取带参数的 sql 语句
默认情况下,toSql 获取到的 sql 里面的参数使用 "?" 代替的,如下: DB::table('user')->where('id', 1)->toSql(); ...
- Python入门 语法
Python入门 语法 语言介绍 对象,类型,值 编码规范 https://www.python.org/dev/peps/pep-0008/ 一.语言介绍 编程语言: 机器语言,汇编语言,高级语言 ...
- java基础-BigInteger类常用方法介绍
java基础-BigInteger类常用方法介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.BigInteger类概述 Java中long型为最大整数类型,对于超过long ...
- 页面缓存之Meta http-equiv属性详解
http-equiv顾名思义,相当于http的文件头作用,它可以向浏览器传回一些有用的信息,以帮助正确和精确地显示网页内容,与之对应的属性值为content,content中的内容其实就是各个参数的变 ...
- vue+element 切换正式和测试环境
1.package.json { "name": "element-starter", "description": "A Vue ...
- nova-compute源码分析
源码版本:H版 首先看启动脚本如下: /usr/bin/nova-compute import sys from nova.cmd.compute import main if __name__ == ...
- [转]windows下安装python MySQLdb及问题解决
转自 https://blog.csdn.net/ping523/article/details/54135228#commentBox 之前按照网络上搜罗的教程安装了python-mysql(1.2 ...
- 6.redis的分布式锁
https://www.cnblogs.com/linjiqin/p/8003838.html
- bzoj千题计划149:bzoj2527: [Poi2011]Meteors
http://www.lydsy.com/JudgeOnline/problem.php?id=2527 整体二分 区间加,单点查,树状数组维护差分序列 注意 累积可能会爆long long,所以一满 ...
- bzoj千题计划116:bzoj1025: [SCOI2009]游戏
http://www.lydsy.com/JudgeOnline/problem.php?id=1025 题目转化: 将n分为任意段,设每段的长度分别为x1,x2,…… 求lcm(xi)的个数 有一个 ...