poj2955括号匹配 区间DP
| 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题. 需要注意第一种情况不管是否匹配,都要枚举k来更新答案,比如: "()()()":dp[0][5]=dp[1][4]+2=4,枚举k,k=1时,dp[0][1]+ ...
随机推荐
- ArrayList实现线程安全的blogs
ArrayList是线程不安全的,轻量级的.如何使ArrayList线程安全? 1.继承Arraylist,然后重写或按需求编写自己的方法,这些方法要写成synchronized,在这些synchro ...
- 网页缩放对 FLASH的影响
目前新出的,和升级的浏览器,都加了页面放大功能, 这些功能是对FLASH有影响的,表现在 flash在获取stage.stageWidth时,数值会按相应比例有变化 本人在用flex4.6开发时,自定 ...
- hibernate考试题
1.在Hibernate中,以下关于主键生成器说法错误的是(C). A.increment可以用于类型为long.short或byte的主键 long,short,byte都是特殊的int类型 B.i ...
- https网站跳转到http网站时,referrer获取不到的问题
工作中,有一个活动列表页A,要链接到具体的活动详情页B,A页面放在https网站上,B页面放在http网站上,从https跳转到http网站时,为了用户隐私安全信息,浏览器默认不传送referrer ...
- domReady方法(dom加载完成执行回调)
var domReady = function( fn ) { var isReady = false, ready = function(){ if(!isReady){ typeof fn === ...
- Windows系统服务器IIS7.5 Asp.net支持10万请求的设置方法
问题现象 ECS Windows系统服务器基于IIS搭建的网站由于IIS默认的配置,服务器最多只能处理5000个同时请求,访问量大时很容易导致报错: Error Summary: HTTP Erro ...
- CF#345 (Div1)
论蒟蒻如何被cf虐 以下是身败名裂后的题解菌=========== Div1 A.Watchmen 有n个点,每个点有一个坐标.求曼哈顿距离=欧几里得距离的点对数量. 只需要统计x或y一样的点对数量. ...
- 在SecureCRT中使用rz和sz传输文件
首先检查Centos中有没有安装 lrzsz sudo yum install lrzsz 使用yum install的时候碰到一个问题, 不知道是否和虚拟机环境有关 Existing lock /v ...
- 《Node.js开发实战详解》学习笔记
<Node.js开发实战详解>学习笔记 ——持续更新中 一.NodeJS设计模式 1 . 单例模式 顾名思义,单例就是保证一个类只有一个实例,实现的方法是,先判断实例是否存在,如果存在则直 ...
- Linux shell文本过滤
正则表达式 --概念:一种用来描述文本模式的特殊语法 --由普通字符(例如:字符a到z),以及特殊字符(元字符,如/*?等)组成匹配的字符串 --文本过滤工具在某种模式之下,都支持正则表达式 --基本 ...