【题目描述】
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.
【输入】
The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.
【输出】
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”.
【样例输入】
)((())))(()())
【样例输出】
6 2
【样例输入】
))(
【样例输出】
0 1
题目链接:https://codeforces.com/contest/5/problem/C
代码如下:
#include <bits/stdc++.h>
using namespace std;
static const int MAXN=1000000;
vector<int> v;
int res[MAXN+10];
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0),cout.tie(0);
string s;
cin>>s;
int maxlen=0,cnt=1;
for(int i=0;i<(int)s.size();i++)
{
if(s[i]=='(') v.push_back(i);
else
{
if(v.empty()) continue;
int len=i-v.back()+1+res[v.back()-1];
if(len>maxlen)
{
maxlen=len;
cnt=1;
}
else if(len==maxlen) cnt++;
res[i]=len;
v.pop_back();
}
}
cout<<maxlen<<" "<<cnt<<endl;
return 0;
}