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.

【输入】
The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.

【输出】
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”.

【样例输入】
)((())))(()())

【样例输出】
6 2

【样例输入】
))(

【样例输出】
0 1

题目链接:https://codeforces.com/contest/5/problem/C

代码如下:

#include <bits/stdc++.h>
using namespace std;
static const int MAXN=1000000;
vector<int> v;
int res[MAXN+10];
int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0),cout.tie(0);
    string s;
    cin>>s;
    int maxlen=0,cnt=1;
    for(int i=0;i<(int)s.size();i++)
    {
        if(s[i]=='(') v.push_back(i);
        else
        {
            if(v.empty()) continue;
            int len=i-v.back()+1+res[v.back()-1];
            if(len>maxlen)
            {
                maxlen=len;
                cnt=1;
            }
            else if(len==maxlen) cnt++;
            res[i]=len;
            v.pop_back();
        }
    }
    cout<<maxlen<<" "<<cnt<<endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值