CF 5C Longest Regular Bracket Sequence

本文介绍了一种使用栈和哈希表(map)的数据结构解决括号匹配问题的方法,并通过C++实现了一个简单的程序来统计字符串中有效括号的最大连续配对长度及出现次数。
部署运行你感兴趣的模型镜像

把所有规则括号都记录起来即可!然后统计连续的规则串,用map来记录!用栈模拟!

不难

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <vector>
#include <limits.h>
#include <stack>
#include <map>
using namespace std;
stack<int> s;
int cot[1000000];
map <int,int> m;
int main()
{
    string str;
    int i,j,len;
    int top;
    while(cin>>str)
    {
        len = str.size();
        memset(cot,0,sizeof(cot));
        top = 0;
        for(i = 0;i < len;i ++)
        {
            if(str[i] == '(')
            {
                s.push(i);
            }
            else
            {
                if(s.empty()) continue;
                top += 2;
                cot[s.top()] = cot[i] = 1 ;
                s.pop();
            }
        }
        //for(i = 0;i < len;i ++) cout<<cot[i];cout<<endl;
        top = 0;
        for(i = 0;i < len;i ++)
        {
            if(cot[i] == 0) {m[top]++;top = 0;}
            else top ++;
        }
        if(top!=0)
        m[top] ++;
        int anslen = 0,anssum = 1;
        map<int,int>::iterator   it;
        for(it = m.begin();it != m.end();it ++)
        {
            if(it->first > anslen) {anslen = it->first;anssum = it->second;}
        }
        cout<<anslen<<" "<<anssum<<endl;
    }

}


您可能感兴趣的与本文相关的镜像

ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值