CodeForces 5C. Longest Regular Bracket Sequence
2015-04-13 20:15
411 查看
C. Longest Regular Bracket Sequence
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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".
Sample test(s)
input
)((())))(()())
output
6 2
input
))(
output
0 1
先将所有数据累加起来,'('代表 -1 , ')'代表 1
此时可以把data中的数据想象成函数图象,下标为横坐标,本题即求此图象两个相等函数值的最大横坐标距离(前提是这两点之间的所有函数值必须小于等于这两点的函数值)。
#include<iostream> #include<cstdio> using namespace std; int len,cur,s,ans_len,ans_sum; char c[1000005]; int data[1000005]; int main(){ while(scanf("%c",&c[1])!=EOF){ cur = 1; data[0]=0; do{ if(c[cur]!='\n') { if(c[cur]=='(') data[cur] = data[cur-1] - 1; else if(c[cur]==')') data[cur] = data[cur-1] + 1; cur++; scanf("%c",&c[cur]); } else break; }while(1); ans_len=len=s=ans_sum=0; for(int i=1;i<cur;i++) { if(data[i]>data[s]) { s = i; if(ans_len<len) { ans_len = len; ans_sum = 1; } else if(ans_len==len&&len!=0) { ans_sum++; } len = 0; } else { len++; } } if(data[cur-1]==data[s]) { if(ans_len==0) ans_len = len; if(cur-1-s==ans_len) ans_sum++; } else if(data[cur-1]<data[s]) { for(int i=1;i<=cur-s;i++) { if(c[cur-i]==')') data[i]=data[i-1] - 1; else if(c[cur-i]=='(') data[i]=data[i-1] + 1; } cur = cur-s+1; s = len = 0; for(int i=1;i<cur;i++) { //这里要注意!!!!需要再循环一遍!!!!! if(data[i]>data[s]) { s = i; if(ans_len<len) { ans_len = len; ans_sum = 1; } else if(ans_len==len&&len!=0) { ans_sum++; } len = 0; } else { len++; } } } if(ans_len==0) ans_sum=1; printf("%d %d\n",ans_len,ans_sum); } }
相关文章推荐
- Codeforces 5C Longest Regular Bracket Sequence
- codeforces 5C Longest Regular Bracket Sequence
- CodeForces 5C Longest Regular Bracket Sequence
- Codeforces 5C Longest Regular Bracket Sequence 题解
- 【CodeForces 5C】【贪心】【dp】Longest Regular Bracket Sequence
- CodeForces 5C Longest Regular Bracket Sequence
- codeforces 5C Longest Regular Bracket Sequence(dp+技巧)【最长连续括号模板】
- codeforces 5C Longest Regular Bracket Sequence -- 贪心
- CodeForces 5C - Longest Regular Bracket Sequence
- Codeforces 5C Longest Regular Bracket Sequence [贪心] [DP]
- CodeForces - 5C Longest Regular Bracket Sequence (动态规划)
- [CF5C]Longest Regular Bracket Sequence
- 5C-codeforce Longest Regular Bracket Sequence
- CF 5C Longest Regular Bracket Sequence
- codeforce 5C Longest Regular Bracket Sequence
- CF_5C_LongestRegularBracketSequence
- codeforcres 5C Longest Regular Bracket Sequence 队列优化 栈
- Review of Codeforces 5C. Longest Regular Bracket Sequence
- Longest Regular Bracket Sequence -括号处理
- Codeforces 612C: Replace To Make Regular Bracket Sequence(栈)