首页 > 其他 > 详细

CodeForces 5C Longest Regular Backet sequence

时间:2018-08-02 22:32:26      阅读:177      评论:0      收藏:0      [点我收藏+]

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\n");
	else
	{
		for(int i=1;i<=len;i++) if(dp[i]==Max) ans++;
		printf("%d %d\n",Max,ans);	
	}
	return 0;
}

  

CodeForces 5C Longest Regular Backet sequence

原文:https://www.cnblogs.com/songorz/p/9409752.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!