Longest Regular Bracket Sequence

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 $ 10^{6} $ .

输出格式

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”.

样例 #1

样例输入 #1

)((())))(()())

样例输出 #1

6 2

样例 #2

样例输入 #2

))(

样例输出 #2

0 1

思路

这种题一看是括号有关的题,跟括号有关的题叫做括号序列,对于括号序列的题我们可以用动态规划+栈来做。

具体做法:

状态表示:f[i]表示以i结尾的最长合法序列的方案数

状态转移:f[i]=i-j+1+f[j-1](其中j是上一次匹配的结尾)

上一次匹配的结尾我们就得用栈来维护了。

但题目到这里还没有结束,题目还要记录下标,因此我们就想到类似桶排序的方式。

特别注意:此时桶排序中括号内(也就是它的下标表示的是i结尾的最长合法序列的方案数(也就是方案数)),桶排序的值指的是这种相同序列的个数。

参考:
请添加图片描述

代码

#include<iostream>
#include<algorithm>
#include<cstring>
#include<stack>

using namespace std;

const int N = 1e6+10;

int f[N];
char s[N];
int t[N];
stack<int>stk;//里面我直接放下标多好
int n;

int main(){
    cin>>s+1;
    
    n=strlen(s+1);
    
    for(int i=1;i<=n;i++){
        if(s[i]=='(')stk.push(i);
        else if(!stk.empty()){
            int j=stk.top();
            t[f[i]=i-j+1+f[j-1]]++;
            stk.pop();
        }
    }
    //倒着找
    for(int i=n;i>=0;i--){
        if(t[i]){
            cout<<i<<' '<<t[i];
            return 0;
        }
    }
    
    cout<<"0 1";
    return 0;
}

  • 24
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

green qwq

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值