(CodeForces - 5C)Longest Regular Bracket Sequence(dp+栈)(最长连续括号模板)
(CodeForces - 5C)Longest Regular Bracket Sequence
time limit per test:2 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output
This is yet another problem dealing with regular bracket sequences.
We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.
You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.
Input
The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.
Output
Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing “0 1”.
Examples
input
)((())))(()())
output
6 2
input
))(
output
0 1
题目意思:
题目的意思就是给你一个小括号的字符串,问你最长的合法括号序列的长度是多少?和有几个这样合法的最长的括号序列
比如:
((())) 这个长度是6,3个左3个右
又比如样例:
)((())))(()())
所以样例的最长长度是6,有两个这样的最长长度的括号段
做法:利用栈进行括号的匹配,加上dp数组记录
dp[i]:位置为i的右括号")"结尾的最长合法括号子序列的长度
dp[i]=dp[temp-1]+i-(temp-1)
其中temp表示与位置为i的右括号匹配的左括号的位置(栈记录了)
code:
#include <iostream>
#include <stdio.h>
#include<memory>
#include<stack>
#include<string.h>
#include<algorithm>
using namespace std;
#define max_v 1000005
int dp[max_v];//位置为i的右括号结尾的最长合法括号子序列的长度
//状态转移方程:dp[i]=dp[tmp-1]+i-tmp+1
stack<int> s;
int main()
{
while(!s.empty())
s.pop();
string str;
cin>>str;
int l=str.size();
int ans=,sum=;
for(int i=; i<l; i++)
{
if(str[i]=='(')
s.push(i);
else
{
if(!s.empty())
{
int temp=s.top();
s.pop();
if(temp)
dp[i]=dp[temp-]+i-temp+;
else
dp[i]=dp[]+i-temp+;
if(ans<dp[i])
{
ans=dp[i];
sum=;
}
else if(ans==dp[i])
{
sum++;
}
}
} }
if(ans==)
{
sum=;
}
printf("%d %d\n",ans,sum);
return ;
}
/*
题目意思很简单,就是给以一串括号,要求最长合法括号子序列。 这是典型的括号题,括号题一般都可以用栈+dp解决。 设dp[i]表示位置为i的右括号结尾的最长合法括号子序列的长度,则易得: dp[i]=dp[tmp-1]+i-tmp+1,其中tmp表示与位置为i的右括号匹配的左括号的位置(可以用栈记录)。 */
(CodeForces - 5C)Longest Regular Bracket Sequence(dp+栈)(最长连续括号模板)的更多相关文章
- Codeforces 5C Longest Regular Bracket Sequence(DP+括号匹配)
题目链接:http://codeforces.com/problemset/problem/5/C 题目大意:给出一串字符串只有'('和')',求出符合括号匹配规则的最大字串长度及该长度的字串出现的次 ...
- CodeForces 5C Longest Regular Backet sequence
This is yet another problem dealing with regular bracket sequences. We should remind you that a brac ...
- Codeforces Beta Round #5 C. Longest Regular Bracket Sequence 栈/dp
C. Longest Regular Bracket Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.c ...
- 贪心+stack Codeforces Beta Round #5 C. Longest Regular Bracket Sequence
题目传送门 /* 题意:求最长括号匹配的长度和它的个数 贪心+stack:用栈存放最近的左括号的位置,若是有右括号匹配,则记录它们的长度,更新最大值,可以在O (n)解决 详细解释:http://bl ...
- CodeForces - 612C Replace To Make Regular Bracket Sequence 压栈
C. Replace To Make Regular Bracket Sequence time limit per test 1 second memory limit per test 256 m ...
- 【Codeforces】CF 5 C Longest Regular Bracket Sequence(dp)
题目 传送门:QWQ 分析 洛谷题解里有一位大佬讲的很好. 就是先用栈预处理出可以匹配的左右括号在数组中设为1 其他为0 最后求一下最长连续1的数量. 代码 #include <bits/std ...
- Codeforces1132A——Regular Bracket Sequence(水题)
Regular Bracket Sequence time limit per test:1 second memory limit per test:256 megabytes input:stan ...
- Educational Codeforces Round 4 C. Replace To Make Regular Bracket Sequence 栈
C. Replace To Make Regular Bracket Sequence 题目连接: http://www.codeforces.com/contest/612/problem/C De ...
- Almost Regular Bracket Sequence CodeForces - 1095E (线段树,单点更新,区间查询维护括号序列)
Almost Regular Bracket Sequence CodeForces - 1095E You are given a bracket sequence ss consisting of ...
随机推荐
- spring的事务传播行为
1.PROPAGATION_REQUIRED:如果当前没有事务,就创建一个新事务,如果当前存在事务,就加入该事务,该设置是最常用的设置. 比如说,ServiceB.methodB的事务级别定义为PRO ...
- 任意表格(table)实现拖动列(column)改变列大小
直接上代码吧,原理可以看我上一篇博文.本实现基于jquery,完美实现拖动改变表格的列大小功能,只需将代码放置在你页面的底部即可(jquery必须先引入). $(function () { var i ...
- Java---工欲善其事必先利其器(准备篇)
Java API 1.7链接:http://pan.baidu.com/s/1cKUaKY 密码:116m Eclispse链接:http://pan.baidu.com/s/1mh6MoL6 密码: ...
- AndroidVideoCache 框架源码分析
1.简析: 在客户端播放视频的使用,容易出现这样的一个问题.在网络状况不好的情况下,视频流很容易卡顿或者中断,即使播放软件本身有一点的缓存能力,但是这个往往不够,造成播放失败,卡顿. AndroidV ...
- C Primer Plus note5
error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token| 遇到这种情况,不要看这里显示了三个错误,就很着急.静 ...
- JS常见的几种数组去重方法
总结一下JS中用到的数组去重的方法 方法一: 该方法利用对象的属性值不能相同: function arrDelLikeElement (array) { const result = []; con ...
- Linux基础之-元字符
Bash中的特殊字符,键盘上能敲出来的特殊字符都有其特殊意义,强调一点:元字符是被shell解释的. 1. '',取命令的执行结果 [root@MiWiFi-R3-srv ~]# ls4.txt an ...
- arcgis10.sp5下载地址
http://support.esrichina.com.cn/2012/0716/1649.html
- Ubuntu 18.04 的网络配置
netplan简介 目前,ubuntu18.04上使用了netplan 作为网络配置工具:在终端上配置网络参数跟之前的版本有比较大的差别 Netplan工作流程如下图所示:通过读取 /etc/net ...
- 谈谈CSS性能
CSS性能优化 1.衡量属性和布局的消耗代价: 2.探索W3C的性能优化新规范: 3.用测试数据判断优化策略. 慎重选择高消耗的样式 1.box-shadows; 2.border-radius; 3 ...