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]+ ...
随机推荐
- ANE原生代码的调试(安卓)
忙了一天终于有空继续这篇教程了. ANE的原生代码的调试其实在Adobe的官网有介绍的,但是同样很含糊,我摸索了一段时间现在记录下我的心得. 首先你得安装Eclipse,然后你得启动Eclipse 然 ...
- Android使用Java Mail API发送邮件
最近在考虑为已经有的一个应用程序增加一个用户反馈的功能,用户可以通过反馈功能将用户的意见和建议.程序出现的问题以一种更符合用户习惯的方式反馈回来.网上也有一些实现好的反馈程序的服务,包括bug的提交. ...
- git删除分支|查看分支动态
git不能在当前分支下删除你当前所在的分支,比如你要删除new分支,而现在正在处于new分支下,则执行git branch -d new的时候会报错 error: Cannot delete bran ...
- Entity Framework 迁移命令 详解
一.Entity Framework 迁移命令(get-help EntityFramework) Enable-Migrations 启用迁移 Add-Migration 为挂起的Model变化添加 ...
- jboss:跟踪所有sql语句及sql参数
默认情况下,hibernate/JPA 在server.log中记录的SQL语句,参数都是用?代替的,这样不太方便. 网上留传的p6spy在最新的jboss上(EAP 6.0+版本)貌似已经不起作用了 ...
- Vue系列: 如何通过组件的属性props设置样式
比如我们要在vue中显示百度地图,然后将相关的代码包装成组件,然后需要由外部来设置组件的高度,关于props的介绍,可以参考: http://cn.vuejs.org/guide/components ...
- ios蓝牙开发(四)app作为外设被连接的实现-转发
代码下载: 原博客中大部分示例代码都上传到了github,地址是:https://github.com/coolnameismy/demo. 再上一节说了app作为central连接periphera ...
- c++返回值 注意事项
1.不要返回指向局部变量或临时对象的引用.函数执行完毕后,局部变量和临时对象会消失,引用将指向不存在的数据 2.返回指向const对象的引用 使用const引用的常见原因是旨在提高效率,但对于何时采用 ...
- Linux 中 Weblogic 启动和关闭
a.start weblogic1)找到 /Oracle/Middleware/user_projects/domains/ 用户_domain目录. nohup ./startWebLogic.sh ...
- Theano2.1.2-基础知识之第一步:代数
来自:http://deeplearning.net/software/theano/tutorial/adding.html Baby Steps - Algebra 一.两个标量相加 在学习the ...