CodeForces 5C Longest Regular Backet sequence

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
题解:DP。用栈存每一个‘(’的下标,遇到一个')',判断一下栈是否为空,若为空,则继续往后,若不为空,则新增加长度为:i-temp+1;
参考代码为:
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+10;
char s[maxn];
int dp[maxn];
int main()
{
	scanf("%s",s+1); stack<int> st;
	int len=strlen(s+1),Max=1,ans=0;
	for(int i=1;i<=len;i++)
	{
		if(s[i]=='(') st.push(i);
		else 
		{
			if(!st.empty())
			{
				int temp=st.top(); st.pop();
				dp[i]=dp[temp-1]+i-temp+1;
				Max=max(dp[i],Max);
			}
		}
	}
	if(Max==1) printf("0 1
");
	else
	{
		for(int i=1;i<=len;i++) if(dp[i]==Max) ans++;
		printf("%d %d
",Max,ans);	
	}
	return 0;
}

  

原文地址:https://www.cnblogs.com/csushl/p/9409752.html