Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5424 | Accepted: 2909 |
Description
We give the following inductive definition of a “regular brackets” sequence:
- the empty sequence is a regular brackets sequence,
- if s is a regular brackets sequence, then (s) and [s] are regular brackets sequences, and
- if a and b are regular brackets sequences, then ab is a regular brackets sequence.
- no other sequence is a regular brackets sequence
For instance, all of the following character sequences are regular brackets sequences:
(), [], (()), ()[], ()[()]
while the following character sequences are not:
(, ], )(, ([)], ([(]
Given a brackets sequence of characters a1a2 … an, your goal is to find the length of the longest regular brackets sequence that is a subsequence of s. That is, you wish to find the largest m such that for indices i1, i2, …, im where 1 ≤ i1 < i2 < … < im ≤ n, ai1ai2 … aim is a regular brackets sequence.
Given the initial sequence ([([]])]
, the longest regular brackets subsequence is [([])]
.
Input
The input test file will contain multiple test cases. Each input test case consists of a single line containing only the characters (
, )
, [
, and ]
; each input test will have length between 1 and 100, inclusive. The end-of-file is marked by a line containing the word “end” and should not be processed.
Output
For each input case, the program should print the length of the longest possible regular brackets subsequence on a single line.
Sample Input
((()))
()()()
([]])
)[)(
([][][)
end
Sample Output
6
4
0
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<string>
#include<time.h>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 1000000001
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const int MAXN = ;
int dp[MAXN][MAXN];
char s[MAXN];
int ok(char k1,char k2)
{
if(k1 == '(' && k2 == ')')
return ;
if(k1 == '[' && k2 == ']')
return ;
return ;
}
int main()
{
while(~scanf("%s",s+)){
if(s[] == 'e')break;
int len = strlen(s+);
int ans = ;
memset(dp,,sizeof(dp));
for(int i = ; i <= len; i++){
for(int j = ; j <= len - i + ; j++){
if(i == && ok(s[j],s[j+i-])){
dp[j][i] = ;
}
else if(i > ){
int p = ;
for(int k = ; k <= i - ; k++){
p = max(p,dp[j+][k]+dp[j+k+][i-k-]);
//cout<<i<<endl;
//cout<<dp[j+1][k]<<' '<<j+1<<' '<<k<<' '<<endl;
//cout<<dp[j+k+1][i-k-1]<<' '<<j+k+1<<' '<<i-k-1<<' '<<endl;
}
if(ok(s[j],s[j+i-])){
p++;
}
dp[j][i] = max(p,dp[j][i]);
for(int k = ; k <= i; k++){
dp[j][i] = max(dp[j][i],dp[j][k]+dp[j+k][i-k]);
}
}
}
}
printf("%d\n",dp[][len] * );
}
return ;
}
poj2955括号匹配 区间DP的更多相关文章
-
括号匹配 区间DP (经典)
描述给你一个字符串,里面只包含"(",")","[","]"四种符号,请问你需要至少添加多少个括号才能使这些括号匹配起来 ...
-
poj 2955 括号匹配 区间dp
Brackets Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6033 Accepted: 3220 Descript ...
-
poj 2955 Brackets 括号匹配 区间dp
题意:最多有多少括号匹配 思路:区间dp,模板dp,区间合并. 对于a[j]来说: 刚開始的时候,转移方程为dp[i][j]=max(dp[i][j-1],dp[i][k-1]+dp[k][j-1]+ ...
-
[poj2955/nyoj15]括号匹配(区间dp)
解题关键:了解转移方程即可. 转移方程:$dp[l][r] = dp[l + 1][r - 1] + 2$ 若该区间左右端点成功匹配.然后对区间内的子区间取max即可. nyoj15:求需要添加的最少 ...
-
UVA 1626 Brackets sequence(括号匹配 + 区间DP)
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105116#problem/E 题意:添加最少的括号,让每个括号都能匹配并输出 分析:dp ...
-
HDU4632 Poj2955 括号匹配 整数划分 P1880 [NOI1995]石子合并 区间DP总结
题意:给定一个字符串 输出回文子序列的个数 一个字符也算一个回文 很明显的区间dp 就是要往区间小的压缩! #include<bits/stdc++.h> using namesp ...
-
POJ2955:Brackets(区间DP)
Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...
-
TZOJ 3295 括号序列(区间DP)
描述 给定一串字符串,只由 “[”.“]” .“(”.“)”四个字符构成.现在让你尽量少的添加括号,得到一个规则的序列. 例如:“()”.“[]”.“(())”.“([])”.“()[]”.“()[( ...
-
POJ2955 Brackets(区间DP)
给一个括号序列,求有几个括号是匹配的. dp[i][j]表示序列[i,j]的匹配数 dp[i][j]=dp[i+1][j-1]+2(括号i和括号j匹配) dp[i][j]=max(dp[i][k]+d ...
随机推荐
-
cocos2d-x 内存管理浅析
Cocos2d-x用create创建对象, 这个方法已经被引擎封装成一个宏定义了:CREATE_FUNC, 下面是这个宏定义的实现: #define CREATE_FUNC(__TYPE__) \ ...
-
Drupal 7.23:函数drupal_alter()注释
/** * Passes alterable variables to specific hook_TYPE_alter() implementations. * * This dispatch fu ...
-
Java Socket编程readLine返回null,read返回-1的条件
客户端正常关闭socket的时候,服务器端的readLine()方法会返回null,或者read()方法会返回-1
-
通过web代理进行跨域访问,http请求返回超时的问题定位
[现象] 在ajax通过web代理跨域访问时,http第一次登陆时正常,但是第二次再下发其他命令的时候总是返回 java.net.SocketTimeoutException: Read timed ...
-
JavaScript的DOM编程--05--获取文本节点
获取文本节点: 1). 步骤: 元素节点 --> 获取元素节点的子节点 2). 若元素节点只有文本节点一个子节点, 例如 <li id="bj" name=" ...
-
配置python虚拟环境Virtualenv及pyenv
pyenv pyenv 可以让机器安装各种不同版本的python pyenv install --list 查看可以安装的python版本 pyenv versions 查看已安装的python版本 ...
-
Kettle根据时间戳同步数据实现
1 Kettle总体步骤 由于Kettle自身的特殊性以及在多个步骤中kettle自身处理数据库事务的特殊性,尝试了很多种方案,最终确定暂使用如下方案. 1.使用此方案可以解决kettle本身数据库事 ...
-
offset系列、scroll系列与client系列
offset系列: offsetLeft:获取元素距离最左边的距离,自身的margin包括在内,不包括自身的border offsetTop:获取元素距离最上边的距离,自身的margin包括在内,不包 ...
-
JavaEE 之 Spring Data JPA(二)
1.JPQL a.定义:Java持久化查询语言(JPQL)是一种可移植的查询语言,旨在以面向对象表达式语言的表达式,将SQL语法和简单查询语义绑定在一起·使用这种语言编写的查询是可移植的,可以被编译成 ...
-
前端开发---HTML---标签
HTML的标签内容 1.index <!--声明文档的类型 标记该文档为HTML5的文件--> <!DOCTYPE html> <!-- 页面的根节点 --> &l ...