CodeForces 5C Longest Regular Backet sequence

Posted songorz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}

  



以上是关于CodeForces 5C Longest Regular Backet sequence的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces-5C. Longest Regular Bracket Sequence

CodeForces - 616D Longest k-Good Segment

CodeForces 632D Longest Subsequence

Codeforces 1304B. Longest Palindrome

Longest Subsequence CodeForces - 632D (lcm)

Codeforces 1304D. Shortest and Longest LIS