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 ...
随机推荐
- activiti教程之示例项目activiti-explorer运行_百度经验
https://jingyan.baidu.com/article/4e5b3e19107ad091901e249e.html
- golang interface 类型变量当作某个具体类型使用
比如,我们定义了一个 struct type person struct { Name string `json:"name"` Age int `json:"age&q ...
- SQL Server 2012安装图解
SQL Server 2012 Enterprise Edition安装图解... 第一部分:安装前的准备 1.疑问:一个PC上可以安装多个SQL Server数据库么 答案:可以的.每一个安装的时候 ...
- 线性回归,感知机,逻辑回归(GD,SGD)
线性回归 线性回归是一个回归问题,即用一条线去拟合训练数据 线性回归的模型: 通过训练数据学习一个特征的线性组合,以此作为预测函数. 训练目标:根据训练数据学习参数(w1,w2, ... , wn,b ...
- linux(ubuntu) mysql安装使用
简单的安装一下: sudo apt-get install mysql-server apt-get isntall mysql-client sudo apt-get install libmysq ...
- Matlab周期图法使用FFT实现
参考文章:http://www.cnblogs.com/adgk07/p/9314892.html 首先根据他这个代码和我之前手上已经拥有的那个代码,编写了一个适合自己的代码. 首先模仿他的代码,测试 ...
- noi题库(noi.openjudge.cn) 1.13编程基础之综合应用 T12 分数求和
12:分数求和 描述 输入n个分数并对他们求和,并用最简形式表示.所谓最简形式是指:分子分母的最大公约数为1:若最终结果的分母为1,则直接用整数表示. 如:5/6.10/3均是最简形式,而3/6需要化 ...
- 贪心问题:区间覆盖 NYOJ 喷水装置(二)
喷水装置(二) 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 有一块草坪,横向长w,纵向长为h,在它的橫向中心线上不同位置处装有n(n<=10000)个点状的喷水 ...
- 详细讲解安全升级MySQL的方法
MySQL升级是非常必要的. 我们在Percona Support上列出了关于MySQL升级最佳实践的各种问题.这篇文章推荐了一些不同情况下升级MySQL的方法. 为什么MySQL升级是必须的? 原因 ...
- 20155303 2016-2017-2 《Java程序设计》第七周学习总结
20155303 2016-2017-2 <Java程序设计>第七周学习总结 教材学习中的问题和解决过程 『问题一』:SimpleDateFormat中每个字符的含义都是什么? 『问题一解 ...