Brackets
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
根据规则来进行区间DP
1 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 #include<cmath>
5 #define lli long long int
6 using namespace std;
7 const int MAXN=1001;
8 const int maxn=0x7fffff;
9 void read(int &n)
10 {
11 char c='+';int x=0;bool flag=0;
12 while(c<'0'||c>'9'){c=getchar();if(c=='-')flag=1;}
13 while(c>='0'&&c<='9')
14 x=(x<<1)+(x<<3)+c-48,c=getchar();
15 flag==1?n=-x:n=x;
16 }
17 char s[MAXN];
18 int dp[MAXN][MAXN];
19 int main()
20 {
21 while(scanf("%s",s))
22 {
23 if(s[0]=='e')
24 break;
25 memset(dp,0,sizeof(dp));
26 int l=strlen(s);
27 for(int i=l;i>=0;i--)
28 for(int j=i;j<l;j++)
29 {
30 //dp[i][j]=max(dp[i+1][j],dp[i][j-1]);
31
32 if((s[i]=='('&&s[j]==')')||(s[i]=='['&&s[j]==']'))
33 dp[i][j]=max(dp[i][j],dp[i+1][j-1]+2);
34
35 for(int k=i;k<j;k++)
36 dp[i][j]=max(dp[i][k]+dp[k+1][j],dp[i][j]);
37
38 }
39 printf("%dn",dp[0][l-1]);
40 }
41 return 0;
42 }
- Ubuntu 16.04 Install OpenCV
- Spring Cloud各组件调优参数
- 手把手教你设置 IntelliJ IDEA 的彩色代码主题
- 实用技巧:Hystrix传播ThreadLocal对象(两种方案)
- OpenDaylight与Mininet应用实战之流表操作
- 使用Spring Cloud Feign上传文件
- 用 TensorFlow 让你的机器人唱首原创给你听
- Spring Cloud限流详解(附源码)
- 手动安装Docker 17.06
- 详述使用 IntelliJ IDEA 解决 jar 包冲突的问题
- Spring Cloud各组件重试总结
- js或者php浮点数运算产生多位小数的理解
- 纠错帖:Zuul & Spring Cloud Gateway & Linkerd性能对比
- 你知道 Python 这五个有趣的彩蛋吗?
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- java_流程控制语句、权限修饰符
- [Oracle 故障处理]记一次RMAN备份警告的处理过程
- [Oracle 日常管理]使用oradebug捕获SQL语句
- [Oracle 日常管理]ERRORSTACK使用介绍
- Oracle参数解析(nls_numeric_characters)
- 在Linux系统中安装Tomcat
- java_缓冲流、转换流、序列化流
- Kali内网使用正向shell入侵
- KALI拿到shell后提权操作
- KALI 内网渗透记录
- CentOS7部署Grafana
- CentOS7网卡配置文件详解
- 基于docker搭建jenkins
- [周末往期回顾]Oracle Data Guard 参数介绍
- 程序员必备CDN加速jsDelivr+Gihub远程仓库