Brackets(括号最大匹配问题(区间dp))
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
- 代码:
- #include<cstdio>
- #include<cstring>
- #include<iostream>
- #include<algorithm>
- #include<queue>
- #include<stack>
- #include<map>
- #include<set>
- #include<vector>
- #include<cmath>
- const int maxn=1e5+;
- typedef long long ll;
- using namespace std;
- string str;
- int dp[][];
- int main()
- {
- while(cin>>str)
- {
- if(str=="end")
- {
- break;
- }
- else
- {
- int n=str.length();
- for(int t=;t<n;t++)
- {
- dp[t][t]=;
- }
- for(int t=;t<n-;t++)
- {
- if((str[t]=='['&&str[t+]==']')||(str[t]=='('&&str[t+]==')'))
- {
- dp[t][t+]=;
- }
- else
- {
- dp[t][t+]=;
- }
- }
- for(int r=;r<=n;r++)
- {
- for(int i=;i<n;i++)
- {
- int j=i+r-;
- if(j>n)
- break;
- if((str[i]=='['&&str[j]==']')||(str[i]=='('&&str[j]==')'))
- {
- dp[i][j]=dp[i+][j-]+;
- }
- else
- dp[i][j]=;
- for(int k=i;k<j;k++)
- {
- dp[i][j]=max(dp[i][j],dp[i][k]+dp[k+][j]);
- }
- }
- }
- printf("%d\n",dp[][n-]);
- }
- }
- return ;
- }
Brackets(括号最大匹配问题(区间dp))的更多相关文章
- POJ - 2955 Brackets括号匹配(区间dp)
Brackets We give the following inductive definition of a “regular brackets” sequence: the empty sequ ...
- 括号序列(区间dp)
括号序列(区间dp) 输入一个长度不超过100的,由"(",")","[",")"组成的序列,请添加尽量少的括号,得到一 ...
- poj2955:括号匹配,区间dp
题目大意: 给一个由,(,),[,]组成的字符串,其中(),[]可以匹配,求最大匹配数 题解:区间dp: dp[i][j]表示区间 [i,j]中的最大匹配数 初始状态 dp[i][i+1]=(i,i+ ...
- POJ 2955 Brackets --最大括号匹配,区间DP经典题
题意:给一段左右小.中括号串,求出这一串中最多有多少匹配的括号. 解法:此问题具有最优子结构,dp[i][j]表示i~j中最多匹配的括号,显然如果i,j是匹配的,那么dp[i][j] = dp[i+1 ...
- POJ 2955 括号匹配,区间DP
题意:给你一些括号,问匹配规则成立的括号的个数. 思路:这题lrj的黑书上有,不过他求的是添加最少的括号数,是的这些括号的匹配全部成立. 我想了下,其实这两个问题是一样的,我们可以先求出括号要匹配的最 ...
- A - Brackets POJ - 2955 (区间DP模板题)
题目链接:https://cn.vjudge.net/contest/276243#problem/A 题目大意:给你一个字符串,让你求出字符串的最长匹配子串. 具体思路:三个for循环暴力,对于一个 ...
- 区间dp总结
poj 1141 Brackets Sequence 基础的区间dp题,注意dp边缘的初始化,以及递归过程中的边界 poj 2955 Brackets 依旧注意初始化,水题 hdu 4745 Two ...
- 区间DP 基本题集
51 Nod 1021 石子归并 模板题,敲就完事了,注意一下这种状态转移方程有个四边形的优化(时间) #include <cstdio> #include <iostream> ...
- CF 149D Coloring Brackets(区间DP,好题,给配对的括号上色,求上色方案数,限制条件多,dp四维)
1.http://codeforces.com/problemset/problem/149/D 2.题目大意 给一个给定括号序列,给该括号上色,上色有三个要求 1.只有三种上色方案,不上色,上红色, ...
- poj 2955 Brackets 括号匹配 区间dp
题意:最多有多少括号匹配 思路:区间dp,模板dp,区间合并. 对于a[j]来说: 刚開始的时候,转移方程为dp[i][j]=max(dp[i][j-1],dp[i][k-1]+dp[k][j-1]+ ...
随机推荐
- Mybais面试题(一)
1.对于Hibernate和MyBatis的区别与利弊,谈谈你的看法 Hibernate与MyBatis的对比: 1.MyBatis非常简单易学,与Hibernate相对较复杂,门槛较高; ...
- property补充
property补充 # class Foo: # @property # def AAA(self): # print('get的时候运行我啊') # # @AAA.setter # def AAA ...
- [COCOS2DX-LUA]0-006.cocos2dx中关于拖动屏幕物件,同时点击home键,返回后页面变黑的问题。
基本信息介绍: 引擎框架: Quick-Cocos2dx-Community-3.6 测试机型: 魅族MX5 问题简介: 有拖动效果的物件,在拖动的工程中,手指不放,同时点击home键退到后台. 再返 ...
- mysql删除数据库提示mysql Error dropping database (can't rmdir './db'...
1.执行ps aux | grep mysql,查看mysql的data目录,比如结果是--datadir=/var/lib/mysql. 2.进入data目录,删除以该数据库为名字的文件夹.cd / ...
- 几行python代码实现钉钉自动打卡,网友:终于告别缺勤了
前言 众所周知因为疫情的原因大家都没有办法上学和上班,“钉钉”这个app起到了重大的作用.学校为了学生成绩开启“钉钉”之路.老师也成一个“合格”的主播,感谢XXX童鞋的礼物.666扣起来 老师为了营造 ...
- Python数据类型-dic,set常见操作
字典常见方法 语法:字典名[新key]=value 功能:给字典增加键值 语法:字典名[字典里存在的key]=新的value 功能:修改字典里的值 功能:删除字典的元素,通过key来进行删除, ...
- RabbitMQ 基础概念进阶
上一篇 RabbitMQ 入门之基础概念 介绍了 RabbitMQ 的一些基础概念,本文再来介绍其中的一些细节和其它的进阶的概念. 一.消息生产者发送的消息不可达时如何处理 RabbitMQ 提供了消 ...
- 关于GPU你必须知道的基本知识
图形处理单元(或简称GPU)会负责处理从PC内部传送到所连接显示器的所有内容,无论你在玩游戏.编辑视频或只是盯着桌面的壁纸,所有显示器中显示的图像都是由GPU进行渲染的. 对普通用户来说,实际上不需要 ...
- k8s使用需认证的私服仓库
本文内容 在K8s中使用需认证的私服仓库需要导入认证信息到集群中,常规导入方式有两种: 使用Docker已登录的仓库密文导入 使用命令行创建Secret对象导入 本文介绍的就是以上两种方法. 使用Do ...
- ThinkPHP 6.0 基础教程 - 安装
ThinkPHP6.0 的环境: PHP >= 7.1.0 我本地环境: Win10 PhpStudy 安装 PhpStudy 如果你已经安装 PhpStudy 或其他环境,请忽略这里 安装方法 ...