码迷,mamicode.com
首页 > 其他好文 > 详细

CodeForces 5C Longest Regular Backet sequence

时间:2018-08-02 23:11:19      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:while   tin   不为   string   clu   har   div   c++   names   

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

标签:while   tin   不为   string   clu   har   div   c++   names   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!