Brackets
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 i1i2, …, im where 1 ≤i1 < i2 < … < im ≤ nai1ai2 … 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

  1. ((()))
  2. ()()()
  3. ([]])
  4. )[)(
  5. ([][][)
  6. end

Sample Output

  1. 6
  2. 6
  3. 4
  4. 0
  5. 6

Source

【题目大意】
最大括号匹配
【思路】
区间dp 枚举长度
【code】
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. using namespace std;
  5. char s[];
  6. int dp[][];
  7. int main()
  8. {
  9. while(gets(s)!=NULL)
  10. {
  11. if(s[]=='e')break;
  12. memset(dp,,sizeof(dp));
  13. int len=strlen(s);
  14. for(int i=;i<=len;i++)
  15. for(int j=,k=i;k<=len;j++,k++)
  16. {
  17. if(s[j]=='('&&s[k]==')'||s[j]=='['&&s[k]==']')
  18. dp[j][k]=dp[j+][k-]+;
  19. for(int p=j;p<=k;p++)
  20. dp[j][k]=max(dp[j][k],dp[j][p]+dp[p+][k]);
  21. }
  22. printf("%d\n",dp[][len-]);
  23. }
  24. return ;
  25. }

  

Brackets(区间dp)的更多相关文章

  1. Codeforces 508E Arthur and Brackets 区间dp

    Arthur and Brackets 区间dp, dp[ i ][ j ]表示第 i 个括号到第 j 个括号之间的所有括号能不能形成一个合法方案. 然后dp就完事了. #include<bit ...

  2. POJ 2995 Brackets 区间DP

    POJ 2995 Brackets 区间DP 题意 大意:给你一个字符串,询问这个字符串满足要求的有多少,()和[]都是一个匹配.需要注意的是这里的匹配规则. 解题思路 区间DP,开始自己没想到是区间 ...

  3. CF149D. Coloring Brackets[区间DP !]

    题意:给括号匹配涂色,红色蓝色或不涂,要求见原题,求方案数 区间DP 用栈先处理匹配 f[i][j][0/1/2][0/1/2]表示i到ji涂色和j涂色的方案数 l和r匹配的话,转移到(l+1,r-1 ...

  4. Brackets(区间dp)

    Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3624   Accepted: 1879 Descript ...

  5. POJ2955:Brackets(区间DP)

    Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...

  6. HOJ 1936&POJ 2955 Brackets(区间DP)

    Brackets My Tags (Edit) Source : Stanford ACM Programming Contest 2004 Time limit : 1 sec Memory lim ...

  7. Code Forces 149DColoring Brackets(区间DP)

     Coloring Brackets time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  8. POJ2955 Brackets —— 区间DP

    题目链接:https://vjudge.net/problem/POJ-2955 Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Su ...

  9. poj 2955 Brackets (区间dp基础题)

    We give the following inductive definition of a “regular brackets” sequence: the empty sequence is a ...

  10. poj2955 Brackets (区间dp)

    题目链接:http://poj.org/problem?id=2955 题意:给定字符串 求括号匹配最多时的子串长度. 区间dp,状态转移方程: dp[i][j]=max ( dp[i][j] , 2 ...

随机推荐

  1. 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 ...

  2. Tomcat服务器解析“GET /JavaWebDemo1/1.jsp HTTP/1.1”

    (2)服务器收到http请求报文,返回http响应报文 Tomcat服务器解析“GET /JavaWebDemo1/1.jsp HTTP/1.1” Tomcat服务器解析“GET /JavaWebDe ...

  3. centos的python2.6.x升级到python2.7.x方法;python2.6.x的版本就不要用了

    python2.6.x的版本,现在使用的很多插件都不支持了.所以如果你的centos还是使用的2.6.x版本,不要犹豫,赶紧升级到2.7.x版本 1.所谓升级,就是再安装一个python2.7.x版本 ...

  4. 全能无线渗透测试工具,一个LAZY就搞定了

    近来一直在研究无线安全方面的东西,特别是在无线渗透测试这块,每次渗透测试时总要来回不停的切换操作和挑选利器,很是麻烦.就想看看是否可以有一款功能全面的集合型工具. 正所谓功夫不负有心人,还真有这么一个 ...

  5. CentOS下常用的 19 条命令

    玩过Linux的人都会知道,Linux中的命令的确是非常多,但是玩过Linux的人也从来不会因为Linux的命令如此之多而烦恼,因为我们只需要掌握我们最常用的命令就可以了.当然你也可以在使用时去找一下 ...

  6. win32收不到F10按键消息解决的方法

    在WM_KEYDOWN中处理F10(VK_F10)消息总是获取不到,后来用spy++监听窗体消息发现按下F10并没有WM_KEYDOWN消息产生,而是产生了WM_SYSKEYDOWN

  7. SQL server 数据存储过程

    创建视图

  8. 【转载】lvs为何不能完全替代DNS轮询

    上一篇文章"一分钟了解负载均衡的一切"引起了不少同学的关注,评论中大家争论的比较多的一个技术点是接入层负载均衡技术,部分同学持这样的观点: 1)nginx前端加入lvs和keepa ...

  9. windows服务 MVC之@Html.Raw()用法 文件流的读写 简单工厂和工厂模式对比

    windows服务   public partial class Service1 : ServiceBase{ System.Threading.Timer recordTimer;public S ...

  10. linux getopt函数详解

    getopt(分析命令行参数)   表头文件 #include<unistd.h> 定义函数 int getopt(int argc,char * const argv[ ],const ...