括号匹配系列(栈的运用)

 题目描述:一个括号序列是指一个由'(', ')', '[', ']'四种字符组成的字符串。

一个只包含数字,加号'+',和上述四种括号的合法算数表达式,去掉数字和加号之后得到的括号序列成为合法的括号序列。我们定义空串也是合法的括号序列。

例如(1) + [2], ([3]), 4 + [5]相应的括号序列 "()[]", "([])", "[]"都是合法的括号序列。而(6 + 7], [8 + ((9对应的"(]", "[(("则是非法的。

字符串 的子串 是指

现在给定一个括号序列,请找出其中的一个子串,使得这个子串是合法的括号序列,且包含的'['数量最多。

输入:一个括号序列s。

输出:第一行,答案要求的子串中包含'['的数量。如果答案是0,输出到此为止,否则:

第二行,两个整数l, r。表示子串第一个字符的位置和最后一个字符的位置。

如果有多个满足条件的字符串,请输出使得子串长度最大的答案。如果长度最大的仍有多个,请输出r最大的。

 ([])

输出:1
0 3
(((]]]输出:0

帮都哥写的题目。。。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
using namespace std;

bool flag1 , flag2;
int l ,r ,l_max,r_max;
struct data
{
    char C;
    int L,R;
    data(int x,int y,char c):L(x),R(y),C(c) {}
    data() {}
} node[500000];
stack<data> st;
int main()
{
#ifdef xxz
    freopen("in.txt","r",stdin);
#endif // xxz
    string str ;
    while(cin>>str)
    {   int cent = 0;
        int len = str.length();
        flag1 = flag2 = false;
        l_max = r_max = l = r = 0;
        for(int i = 0; i < len; i++)
        {
            if(str[i] >= '1' && str[i] <= '9' || str[i] == '+') continue;
            else
            {
                if(st.empty() && str[i]  == '(' || str[i] == '[')
                {
                    st.push(data(i,i,str[i]));

                }
                else if(!st.empty())
                {
                    data p = st.top();
                    char ch =  p.C;
                    if(str[i] == ']')
                    {
                        if(ch != '[')
                        {

                            while(!st.empty()) st.pop();


                        }
                        else
                        {

                            p.R = i;
                            //  cout<<i<<" "<<l_max<<"  "<<r_max<<endl;
                             if(r_max +1== p.L)
                            {
                                r_max = p.R;
                            }
                            else if(p.R - p.L >= r_max - l_max)
                            {
                                r_max = p.R;
                                l_max = p.L;

                            }
                              // cout<<i<<" "<<l_max<<"  "<<r_max<<endl;
                            st.pop();

                        }

                    }
                    else if(str[i]  == '(' || str[i] == '[')
                    {
                        st.push(data(i,i,str[i]));
                    }
                    else if(str[i]  == ')')
                    {
                        if(p.C == '(')
                        {
                            p.R = i;

                            if(r_max+1 == p.L )
                            {
                                r_max = p.R;
                            }
                            else if(p.R - p.L >= r_max - l_max)
                            {
                                r_max = p.R;
                                l_max = p.L;

                            }
                            st.pop();
                        }
                        else
                        {
                            while(!st.empty()) st.pop();

                        }
                    }

                }
            }
        }
        if(r_max - l_max <= 0) cout<<"0"<<endl;
        else
        {
            for(int j = l_max; j <=r_max; j++ )
            {
                if(str[j] == '[') cent++;
            }
            cout<<cent<<endl;
            if(cent != 0) cout<<l_max<<" "<<r_max<<endl;
        }

    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值