CF_5C_LongestRegularBracketSequence
2016-03-30 20:26
555 查看
C. 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
input
output
题目意思求给出的括号串中最长的合法括号序列
这题目最开始想的用1 -1前缀和解决 但是发现很多情况并不能处理
这里可以这样想 首先)肯定是匹配的最近的没有匹配的(
不然一定出现没有匹配的或者交替匹配的
于是可以把(入栈 遇到)则出栈一个
而 目前两个()之间的括号序列一定是匹配的
于是只需要把所有的(的位置入栈 出栈时计算下即可知道当前括号序列的长度
但是 这里注意到一点 一个合法的括号序列并不一定是(…… 序列 ……)这样的形式
也可能是(……)(……)这样的形式
于是 用一个数组管理到目前位置的合法序列长度 于是每次得到匹配的括号更新这个值即可
有点dp思想
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
题目意思求给出的括号串中最长的合法括号序列
这题目最开始想的用1 -1前缀和解决 但是发现很多情况并不能处理
这里可以这样想 首先)肯定是匹配的最近的没有匹配的(
不然一定出现没有匹配的或者交替匹配的
于是可以把(入栈 遇到)则出栈一个
而 目前两个()之间的括号序列一定是匹配的
于是只需要把所有的(的位置入栈 出栈时计算下即可知道当前括号序列的长度
但是 这里注意到一点 一个合法的括号序列并不一定是(…… 序列 ……)这样的形式
也可能是(……)(……)这样的形式
于是 用一个数组管理到目前位置的合法序列长度 于是每次得到匹配的括号更新这个值即可
有点dp思想
#include <iostream> #include <stdio.h> #include <string.h> #include <stack> using namespace std; const int M=1e6+5; char s[M]; int n[M];//保存到当前位置最长的合法序列长度 stack<int> st; int main() { scanf("%s",s+1); int l=strlen(s+1); int ml=0,mn=1; //最大子串长度,子串个数 // int su=0; //前缀和 for(int i=1;i<=l;i++) { if(st.empty()&&s[i]==')') continue; if(s[i]==')') { n[i]=i-st.top()+1+n[st.top()-1]; st.pop(); if(n[i]==ml) mn++; if(n[i]>ml) { ml=n[i]; mn=1; } } else st.push(i); //cout<<"i "<<i<<" nl "<<n[i]<<endl; } printf("%d %d\n",ml,mn); return 0; }
相关文章推荐
- UIRichLabel
- windowbuilder入门
- IOS开发-UI学习-UITextField的各种属性设置
- UIKit层面的动画
- 关于String,StringBuffer与StringBuilder
- dom4j-Quick Start Guide
- UILabel、UITextView自适应得到高度
- ACM程序设计选修课——1043: Radical loves integer sequences(YY)
- 从Long.ValueOf("String")与Long.parseLong("String")看JAVA包装类的封箱与拆箱
- iOS程序的Build过程
- The global shader cache file'X:/XXXX/GlobalShaderCache-PCD3D_SM5.bin' is missing——UE4工程运行失败
- POJ-1679 The Unique MST ( 次小生成树 )
- Long.valueOf()作用
- easyui 获取子节点并修改节点颜色
- IOS学习之——UIEvent 控件的学习
- IOS学习之——UITouch 控件的学习
- IOS学习之——UIResponder
- UIPageViewController用法
- 关于UIView中的坐标转换
- LeetCode之300—-Longest Increasing Subsequence