CodeForces - 5C Longest Regular Bracket Sequence
Posted 西北会法语
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CodeForces - 5C Longest Regular Bracket 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".
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的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces-5C. Longest Regular Bracket Sequence
CodeForces 632D Longest Subsequence
Codeforces 1304B. Longest Palindrome
Longest Subsequence CodeForces - 632D (lcm)