CodeForces 5C. Longest Regular Bracket Sequence

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);
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值