POJ - 2955 Brackets括号匹配(区间dp)
Brackets
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
6
4
0
6
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=;
char s[N];
int f[N][N];
inline bool check(int i,int j){
if(s[i]=='['&&s[j]==']') return ;
if(s[i]=='('&&s[j]==')') return ;
return ;
}
int main(){
while(~scanf("%s",s+)){
if(s[]=='e') break;
memset(f,,sizeof(f));
int n=strlen(s+);
for(int i=n;i>=;i--)
for(int j=i+;j<=n;j++){
if(check(i,j)) f[i][j]=f[i+][j-]+;
for(int k=i;k<=j;k++) f[i][j]=max(f[i][j],f[i][k]+f[k][j]);
}
printf("%d\n",f[][n]);
}
}
POJ - 2955 Brackets括号匹配(区间dp)的更多相关文章
- 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(括号匹配一)
题目链接:http://poj.org/problem?id=2955 题目大意:给你一串字符串,求最大的括号匹配数. 解题思路: 设dp[i][j]是[i,j]的最大括号匹配对数. 则得到状态转移方 ...
- poj 2955 括号匹配 区间dp
Brackets Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6033 Accepted: 3220 Descript ...
- poj2955括号匹配 区间DP
Brackets Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5424 Accepted: 2909 Descript ...
- 括号匹配 区间DP (经典)
描述给你一个字符串,里面只包含"(",")","[","]"四种符号,请问你需要至少添加多少个括号才能使这些括号匹配起来 ...
- POJ 1141 Brackets Sequence (区间DP)
Description Let us define a regular brackets sequence in the following way: 1. Empty sequence is a r ...
- UVA 1626 Brackets sequence(括号匹配 + 区间DP)
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105116#problem/E 题意:添加最少的括号,让每个括号都能匹配并输出 分析:dp ...
- [poj2955/nyoj15]括号匹配(区间dp)
解题关键:了解转移方程即可. 转移方程:$dp[l][r] = dp[l + 1][r - 1] + 2$ 若该区间左右端点成功匹配.然后对区间内的子区间取max即可. nyoj15:求需要添加的最少 ...
- poj 1141 Brackets Sequence(区间DP)
题目:http://poj.org/problem?id=1141 转载:http://blog.csdn.net/lijiecsu/article/details/7589877 定义合法的括号序列 ...
随机推荐
- mysql系列之5.mysql备份恢复
备份数据: mysqldump #mysqldump -uroot -p123456 test > /test_bak.sql #egrep -v "#|\*|--|^$" ...
- 中文WebFont探索
本文主要讲中文webFont的相关知识,包含了业界现状.WebFont优势.实现方案等. 一 业界使用WebFont现状 1.1 英文WebFont使用现状 英文版已使用非常广泛.比较有名的字体库:G ...
- LeetCode 017 4Sum
[题目] Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d ...
- 《转》使用nginx和php实时产生缩略图
在做自动静态化的时候,突然想到下面这个场景,也给出了解决方法.亲,真的很实用,耐心看下去. 当我从后台上传一个截图之后,480*800的截图之后,当时就没有压缩出320*480的小缩略图.好吧 ...
- weblogic开启远程访问的jmx设置
通过jmx远程访问weblogic获取监控jvm的数据,要在weblogic启动的时候设置一些配置,具体如下: 在weblogic的安装目录:{weblogic_home}/wlserver_10.3 ...
- Java for LeetCode 129 Sum Root to Leaf Numbers
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- selenium超时设置
设置超时时间 from selenium import webdriverdriver=webdriver.Chrome()driver.set_page_load_timeout(5)driver. ...
- iOS本地数据存取,看这里就够了
本文授权转载,作者:hosea_zhou(简书) 应用沙盒 1)每个iOS应用都有自己的应用沙盒(应用沙盒就是文件系统目录),与其他文件系统隔离.应用必须待在自己的沙盒里,其他应用不能访问该沙盒 2) ...
- BZOJ 3990 [SDOI2015]排序
题解: 首先很容易看出各个操作是互不影响的,即对于一个合法的操作序列,我们可以任意交换两个操作的位置而不影响合法性. 因此我们可以忽略操作先后的影响,只考虑这个操作是否会出现在操作序列中. 如果用2n ...
- matlab给图片插入说明文字
text(cluster().center(),cluster().center(),num2str(),'color','k') 格式是text(x,y,'说明文字') x,y代表位置