Brackets(区间dp)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8017 | Accepted: 4257 |
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
- 6
- 4
- 0
- 6
Source
- #include<iostream>
- #include<cstdio>
- #include<cstring>
- using namespace std;
- char s[];
- int dp[][];
- int main()
- {
- while(gets(s)!=NULL)
- {
- if(s[]=='e')break;
- memset(dp,,sizeof(dp));
- int len=strlen(s);
- for(int i=;i<=len;i++)
- for(int j=,k=i;k<=len;j++,k++)
- {
- if(s[j]=='('&&s[k]==')'||s[j]=='['&&s[k]==']')
- dp[j][k]=dp[j+][k-]+;
- for(int p=j;p<=k;p++)
- dp[j][k]=max(dp[j][k],dp[j][p]+dp[p+][k]);
- }
- printf("%d\n",dp[][len-]);
- }
- return ;
- }
Brackets(区间dp)的更多相关文章
- Codeforces 508E Arthur and Brackets 区间dp
Arthur and Brackets 区间dp, dp[ i ][ j ]表示第 i 个括号到第 j 个括号之间的所有括号能不能形成一个合法方案. 然后dp就完事了. #include<bit ...
- POJ 2995 Brackets 区间DP
POJ 2995 Brackets 区间DP 题意 大意:给你一个字符串,询问这个字符串满足要求的有多少,()和[]都是一个匹配.需要注意的是这里的匹配规则. 解题思路 区间DP,开始自己没想到是区间 ...
- CF149D. Coloring Brackets[区间DP !]
题意:给括号匹配涂色,红色蓝色或不涂,要求见原题,求方案数 区间DP 用栈先处理匹配 f[i][j][0/1/2][0/1/2]表示i到ji涂色和j涂色的方案数 l和r匹配的话,转移到(l+1,r-1 ...
- Brackets(区间dp)
Brackets Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3624 Accepted: 1879 Descript ...
- POJ2955:Brackets(区间DP)
Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...
- HOJ 1936&POJ 2955 Brackets(区间DP)
Brackets My Tags (Edit) Source : Stanford ACM Programming Contest 2004 Time limit : 1 sec Memory lim ...
- Code Forces 149DColoring Brackets(区间DP)
Coloring Brackets time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- POJ2955 Brackets —— 区间DP
题目链接:https://vjudge.net/problem/POJ-2955 Brackets Time Limit: 1000MS Memory Limit: 65536K Total Su ...
- poj 2955 Brackets (区间dp基础题)
We give the following inductive definition of a “regular brackets” sequence: the empty sequence is a ...
- poj2955 Brackets (区间dp)
题目链接:http://poj.org/problem?id=2955 题意:给定字符串 求括号匹配最多时的子串长度. 区间dp,状态转移方程: dp[i][j]=max ( dp[i][j] , 2 ...
随机推荐
- centos 安装php缓存 apc或zend-opcode
去官方下载apc:pecl.php.net 搜索apc,安装最新的. #wget http://pecl.php.net/get/APC# tar -xzvf APC-3.1.9.tgz#cd AP ...
- Tomcat服务器解析“GET /JavaWebDemo1/1.jsp HTTP/1.1”
(2)服务器收到http请求报文,返回http响应报文 Tomcat服务器解析“GET /JavaWebDemo1/1.jsp HTTP/1.1” Tomcat服务器解析“GET /JavaWebDe ...
- centos的python2.6.x升级到python2.7.x方法;python2.6.x的版本就不要用了
python2.6.x的版本,现在使用的很多插件都不支持了.所以如果你的centos还是使用的2.6.x版本,不要犹豫,赶紧升级到2.7.x版本 1.所谓升级,就是再安装一个python2.7.x版本 ...
- 全能无线渗透测试工具,一个LAZY就搞定了
近来一直在研究无线安全方面的东西,特别是在无线渗透测试这块,每次渗透测试时总要来回不停的切换操作和挑选利器,很是麻烦.就想看看是否可以有一款功能全面的集合型工具. 正所谓功夫不负有心人,还真有这么一个 ...
- CentOS下常用的 19 条命令
玩过Linux的人都会知道,Linux中的命令的确是非常多,但是玩过Linux的人也从来不会因为Linux的命令如此之多而烦恼,因为我们只需要掌握我们最常用的命令就可以了.当然你也可以在使用时去找一下 ...
- win32收不到F10按键消息解决的方法
在WM_KEYDOWN中处理F10(VK_F10)消息总是获取不到,后来用spy++监听窗体消息发现按下F10并没有WM_KEYDOWN消息产生,而是产生了WM_SYSKEYDOWN
- SQL server 数据存储过程
创建视图
- 【转载】lvs为何不能完全替代DNS轮询
上一篇文章"一分钟了解负载均衡的一切"引起了不少同学的关注,评论中大家争论的比较多的一个技术点是接入层负载均衡技术,部分同学持这样的观点: 1)nginx前端加入lvs和keepa ...
- windows服务 MVC之@Html.Raw()用法 文件流的读写 简单工厂和工厂模式对比
windows服务 public partial class Service1 : ServiceBase{ System.Threading.Timer recordTimer;public S ...
- linux getopt函数详解
getopt(分析命令行参数) 表头文件 #include<unistd.h> 定义函数 int getopt(int argc,char * const argv[ ],const ...