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 exceed106.
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
#include<stdio.h>
#include<iostream>
#include<string>
#include<algorithm>
#include<string.h>
#include<map>
#include<math.h>
#include<set>
#include<queue>
#include<vector>
#include<stack>
using namespace std;
int dp[1000001];//dp数组存的是到当前位置(右括号)最长序列的长度
int main()
{
string s;
int i;
stack<int>s1;
while(cin>>s)
{
memset(dp,0,sizeof(dp));
while(!s1.empty())
s1.pop();
for(i=0;i<s.size();i++)
{
if(s[i]=='(')
{
s1.push(i);
}
else//如果是右括号
{
if(!s1.empty())
{
dp[i]=i-s1.top()+1;//计算当前右括号与左括号的距离(长度)
if(s1.top()>0)
{
dp[i]+=dp[s1.top()-1];//加上前边的长度
}
s1.pop();
}
}
}
int max=0,n=1;
for(i=0;i<s.size();i++)
{
if(dp[i]>max)
{
n=1;
max=dp[i];
}
else if(dp[i]==max)
n++;
}
if(max==0)
printf("0 1\n");
else
printf("%d %d\n",max,n);
}
}