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 }
- Maven 项目下slf4j 包冲突问题
- 谷歌教你学 AI-第三讲简单易懂的估算器
- 黑产是如何强刷用户银行卡8.1万元的?
- Spring 必知概念(二)
- 利用iText 组件导出PDF
- 利用POI组件导出Excel的简单示例
- MongoDB 整合spring-data-jpa,spring 4.x
- 【微信开发】 红包接口开发
- 【微信开发】 使用单例设计模式 提供AccessToken 和Jsapi_ticket缓存支持
- @ResponseBody响应JSON 406
- tomcat不能运行或共存多个项目
- Spring Task 定时任务
- 如何为机器学习索引,切片,调整 NumPy 数组
- 逆向工程分析:摩托罗拉安全摄像头究竟有多不安全?
- 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 数组属性和方法
- 六、垃圾回收_判断对象是否存活算法_引用计数法详解
- webpack + express 实现文件精确缓存
- python提取视频中的音频
- Vue3 响应式原理
- vue-mergeable-table 动态生成的可合并行列的表格
- 【GOFLY】基于GO语言gin框架和websocket实现的全开源WEB客服即时通讯管理系统
- edgeXFoundry中modbus通信部署和测试(待续)
- C++面向对象学习之运算符重载(1)
- PAT (Basic Level) Practice (中文)1045 快速排序 (25 分)
- PAT (Advanced Level) Practice 1028 List Sorting (25分)
- CNS图表复现06—根据CellMarker网站进行人工校验免疫细胞亚群
- 【Java】03 流程控制语句
- Artwork (Gym - 102346A)【DFS、连通块】
- PAT (Basic Level) Practice (中文)1046 划拳 (15 分)
- PAT (Advanced Level) Practice 1030 Travel Plan (30分)