(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+栈)(最长连续括号模板)的更多相关文章

  1. Codeforces 5C Longest Regular Bracket Sequence(DP+括号匹配)

    题目链接:http://codeforces.com/problemset/problem/5/C 题目大意:给出一串字符串只有'('和')',求出符合括号匹配规则的最大字串长度及该长度的字串出现的次 ...

  2. CodeForces 5C Longest Regular Backet sequence

    This is yet another problem dealing with regular bracket sequences. We should remind you that a brac ...

  3. 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 ...

  4. 贪心+stack Codeforces Beta Round #5 C. Longest Regular Bracket Sequence

    题目传送门 /* 题意:求最长括号匹配的长度和它的个数 贪心+stack:用栈存放最近的左括号的位置,若是有右括号匹配,则记录它们的长度,更新最大值,可以在O (n)解决 详细解释:http://bl ...

  5. 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 ...

  6. 【Codeforces】CF 5 C Longest Regular Bracket Sequence(dp)

    题目 传送门:QWQ 分析 洛谷题解里有一位大佬讲的很好. 就是先用栈预处理出可以匹配的左右括号在数组中设为1 其他为0 最后求一下最长连续1的数量. 代码 #include <bits/std ...

  7. Codeforces1132A——Regular Bracket Sequence(水题)

    Regular Bracket Sequence time limit per test:1 second memory limit per test:256 megabytes input:stan ...

  8. 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 ...

  9. Almost Regular Bracket Sequence CodeForces - 1095E (线段树,单点更新,区间查询维护括号序列)

    Almost Regular Bracket Sequence CodeForces - 1095E You are given a bracket sequence ss consisting of ...

随机推荐

  1. Java基础(十二)IO输入输出

    一.IO 概述 1.IO 概念 IO:I 代表 Input 输入:O 代表 Output 输出. Java 中 IO 是以流为基础进行输入输出,所有的数据被串行化(保存)写入输出流,或者从输入流读入. ...

  2. 简单的maven项目

    手动建立一个简单的maven项目,不使用eclipse 好吧,第一步:建立一个项目路径,比如在d盘新建一个mavenTest文件夹,在mavenTest文件夹中建立HelloMaven的文件夹,作为项 ...

  3. js判断值是否是数字

    js如何判断值是否是数字 1. isNaN()方法2. 正则表达式var re = /^[0-9]+.?[0-9]*$/; //判断字符串是否为数字 //判断正整数 /^[1-9]+[0-9]*]*$ ...

  4. 提问的智慧 How To Ask Questions The Smart Way

    提问的智慧 How To Ask Questions The Smart Way Copyright © 2001,2006,2014 Eric S. Raymond, Rick Moen 本指南英文 ...

  5. 第4课 简易浏览器-WebViewer组件的使用方法

    做一个手机浏览器,需要哪些组件呢? 一.组件设计 二.组件属性及命名修改 三.逻辑设计 1.导航按钮代码:前进.后退.主页 2.访问网页按钮 1)根据用户在地址栏输入的地址书写,判断书写中是否含有“h ...

  6. canvas.addEventListener()

    对 canvas 元素的事件监听用addEventListener()实现, 但是有一点缺陷是:canvas 不支持键盘事件,为了解决这个问题,可以采用以下两种方案: 方案一: key event - ...

  7. Java基础之this关键字的作用

    this关键字主要存在三种应用 1.this调用本类中的属性,也就是说调用类中的成员变量: 2.this调用本类中的其他方法: 3.this调用本类中的其他构造方法,调用时要放在构造方法的首行,否则会 ...

  8. QueryDSL

    QueryDSL只是通用的查询框架,不支持写操作 查询出来的结果是List,我们不得不做额外的工作将它转化为领域实体

  9. Android-删除指定包名的App

    /** * check and delete the old package app if it exists. */ private void checkOldPackage() { String ...

  10. XISE菜刀V13.0 官网版 XISE菜刀VIP破解版 XISE官网

    诠释: 1. 破解VIP登陆限制 2.去后门 (自查) 下载地址 :https://pan.baidu.com/s/1eR2rUOM 查毒地址:http://a.virscan.org/a3983f3 ...