标签:pre pop har rem string and seq eal other
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".
Example
)((())))(()())
6 2
))(
0 1
1 #include<iostream> 2 #include<string.h> 3 #include<cstdio> 4 #include<stack> 5 6 using namespace std; 7 8 char s[1000005]; 9 int dp[1000005]; 10 stack<int> st; 11 12 int main() 13 { 14 scanf("%s",s); 15 int ans=0,maxx=0,i,l=strlen(s); 16 for(i=0;i<l;++i) 17 { 18 if(s[i]==‘(‘) 19 st.push(i); 20 else 21 { 22 if(!st.empty()) 23 { 24 int t=st.top(); 25 st.pop(); 26 dp[i]=dp[t-1]+i-t+1; 27 if(dp[i]>maxx) 28 { 29 maxx=dp[i]; 30 ans=1; 31 } 32 else if(dp[i]==maxx) 33 ans++; 34 } 35 } 36 } 37 if(!maxx) 38 ans=1; 39 printf("%d %d\n",maxx,ans); 40 41 return 0; 42 }
CodeForces - 5C Longest Regular Bracket Sequence
标签:pre pop har rem string and seq eal other
原文地址:http://www.cnblogs.com/xibeiw/p/7280289.html