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

来源:互联网 发布:英菲克安装软件密码 编辑:程序博客网 时间:2024/05/19 13:58
 题目描述:一个括号序列是指一个由'(', ')', '[', ']'四种字符组成的字符串。

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

例如(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 0
原创粉丝点击