您的位置:首页 > 产品设计 > UI/UE

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);
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: