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

((()))
()()()
([]])
)[)(
([][][)
end

Sample Output

6
6
4
0
6

Source

【题目大意】
最大括号匹配
【思路】
区间dp 枚举长度
【code】
#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 ;
}

  

最新文章

  1. 如何一步一步用DDD设计一个电商网站(九)—— 小心陷入值对象持久化的坑
  2. 【NopCommerce源码架构学习-一】--初识高性能的开源商城系统cms
  3. java上传图片或者文件
  4. 转一篇关于如何在Unity里使用Protobuf
  5. 06-BCD计数器设计与应用——小梅哥FPGA设计思想与验证方法视频教程配套文档
  6. hadoop常用管理员命令
  7. erlang启动参数
  8. Cannot open your terminal &#39;/dev/pts/4&#39; - please check.
  9. RollPagerView的用法:
  10. rowid的作用
  11. [C++]让CPU使用率曲线呈现为正弦曲线(一)
  12. .Net Core Identity外面使用Cookie中间件
  13. bzoj3289
  14. C#多线程-volatile、lock关键字
  15. PHP 5.6 中 Automatically populating $HTTP_RAW_POST_DATA is deprecated and will be removed in a future version
  16. elasticsearch之分词插件使用
  17. Arm11-mini6410入坑
  18. EnumUtil 链表转换工具类
  19. 【转】vue.js表单校验详解
  20. 2018面向对象程序设计(Java)第4周学习指导及要求

热门文章

  1. POJ 1991 Turning in Homework(区间DP)
  2. SpringBoot整合freemarker中自定义标签获取字典表的数据
  3. java并发编程阻塞队列
  4. Go -- 漫谈IM通信架构
  5. Android ZXing 二维码、条形码扫描介绍
  6. 校园网、教育网 如何纯粹访问 IPv6 网站避免收费
  7. 天下文章一大抄 之 修改excel 创建时间
  8. python 学习笔记 13 -- 经常使用的时间模块之time
  9. 基于Office 365 无代码工作流分析-表单基本需求分析!
  10. react map 遍历