(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 ...
随机推荐
- Linux 创建python虚拟环境
使用virtualenv包管理工具来管理虚拟环境 1.安装virtualenv 不知啥原因,第一次安装超时失败,第二次下载到30%超时失败,第三次才安装成功 2.创建虚拟环境 只有python2.7及 ...
- Creator4.2建模心得与技巧1——树的建立与跟随摄像机旋转
Creator建模: 树一般在虚拟现实程序中都用面来实现,一种方法是通过两个面相互垂直成90度叠放在一起,另一种方法是让树面正对着视角一起旋转.这里主要说一下第二种方法. 主要思路:把树面一直正对着摄 ...
- 讲解JavaScript两个圆括号、自调用和闭包函数
一.JavaSript圆括号的使用 先来看一组通过函数声明来定义的函数: 先附代码: 运行结果如下: 这里我们可以看出: Ø 若没有加圆括号,则返回的是这个函数的内容 Ø 若加上圆括号,则返回的是 ...
- mysqlcppconn之ConnectOptionsMap的使用
由来 继上一篇文章, 发现之前写的一篇文章中断线重连部分是错误的, 也是现在翻阅了源码才知道 想要自动重连, 必须使用ConnectOptionsMap才可以 但由于官方代码没有做好导出部分的处理, ...
- Android 触发Button按钮事件的三种方式
1.新创建一个类 2.使用内部类 3.当多个button按钮时,为简化代码而创建的实例listener 贴代码: MainActivity.Java 文件: package com.android. ...
- daemontools检测进程,退出拉起
一.学习的原因: 为了实现在服务异常停止运行后,有一个监控程序能监控到它,并自动重新启动这个服务.以下以tomcat为例子 二.工具supervise Daemontools是一个包含了很多管理Uni ...
- seo搜索引擎优化
1.logo 图片换文字 比较好的方法,就是用背景图呈现,标签中写文字,因为文字能被搜索引擎抓取.(background) <h1><a href="##"> ...
- ajax 请求调用问题
http://localhost/dev/list 和 http://127.0.0.1/dev/list 最近在架构整体常规通用系统的解决方案,遭遇AJAX请求不执行的问题,刚开始以为Spring ...
- win2008 svn 搬迁
公司说电脑不够用,要我们将本地开发用的服务器贡献出来给别人当办公电脑用..汗 将SVN从一个win2008服务器上搬迁到另一个win2008服务器上面. 先将服务器上面的配置好的svn 跟目录备份下来 ...
- java、C语言实现数组模拟栈
java: public class ArrayStack { private int[] data; private int top; private int size; public ArrayS ...