7-6-1 stack 括号匹配

给定仅包含“()[]{}”六种括号的字符串,请你判断该字符串中,括号的匹配是否是合法的,也就是对应括号的数量、嵌套顺序完全正确。

输入格式:

第一行一个整数T(T<=10) 其后T行每行一个字符串只包含[{()}]六种字符(字符串长度2e5以内)

输出格式:

对于每个字符串,匹配输出Yes,否则输出No

输入样例:

2
{()[]}
([)]

输出样例:

Yes No

报段错误代码段
原因:这一段会出现访问非法内存的情况,char a = st.top();赋值时有可能栈空,没有栈顶。

else if((str[i] == ')') || (str[i] == ']') || (str[i] == '}'))
            {
                char a = st.top();
                if((a == '(' && str[i] == ')') || (a == '{' && str[i] == '}') || (a == '[' && str[i] == ']'))
                {
                    st.pop();
                }
                else return false;
            }

AC代码

#include <iostream>
#include <stack>
#include <string>
using namespace std;
stack<char> st;
bool ismatch(string str)
{
    int n = str.length();
    if(n % 2 != 0) return false;
    else
    {
        for(int i = 0; i < n; i++)
        {
            if( (str[i] == '(')|| (str[i] == '[')|| (str[i] == '{'))
            {
                st.push(str[i]);
            }
            else
            {
                if(!st.empty()) //注意先判断栈空情况
                {
                    if((st.top() == '(' && str[i] == ')') || (st.top() == '{' && str[i] == '}') || (st.top() == '[' && str[i] == ']'))
                    {
                        st.pop();
                    }
                    else return false;
                }
                else return false;
            }
        }
    }
    return true;
}
int main()
{
    int T;
    cin >> T;
    string str;
    while(T--)
    {
        cin >> str;
        if(ismatch(str) == false) cout << "No" <<endl;
        else cout << "Yes" <<endl;
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
可以使用STL模板库stack数据结构来实现括号匹配。 具体实现方法如下: 1. 遍历字符串的每个字符。 2. 如果当前字符是左括号(如'('、'{'、'['等),则将其压入。 3. 如果当前字符是右括号(如')'、'}'、']'等),则弹出顶元素,判断是否与当前右括号匹配。如果匹配,则继续遍历下一个字符;如果不匹配,则说明括号匹配,返回false。 4. 如果遍历完字符串后,为空,则说明括号匹配;否则,说明括号匹配,返回false。 下面是一个示例代码: ```c++ #include <iostream> #include <stack> #include <string> using namespace std; bool isMatched(string str) { stack<char> s; for (int i = ; i < str.length(); i++) { char c = str[i]; if (c == '(' || c == '{' || c == '[') { s.push(c); } else if (c == ')' || c == '}' || c == ']') { if (s.empty()) { return false; } char top = s.top(); s.pop(); if ((c == ')' && top != '(') || (c == '}' && top != '{') || (c == ']' && top != '[')) { return false; } } } return s.empty(); } int main() { string str = "{[()]}"; if (isMatched(str)) { cout << "括号匹配" << endl; } else { cout << "括号匹配" << endl; } return ; } ``` 在上面的代码,我们使用了STL模板库stack数据结构来实现括号匹配。具体来说,我们定义了一个名为s的stack<char>类型的,用于存储左括号。在遍历字符串时,如果遇到左括号,则将其压入;如果遇到右括号,则弹出顶元素,判断是否与当前右括号匹配。如果匹配,则继续遍历下一个字符;如果不匹配,则说明括号匹配,返回false。最后,如果遍历完字符串后,为空,则说明括号匹配;否则,说明括号匹配,返回false。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

moumoumouwang

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

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

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

打赏作者

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

抵扣说明:

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

余额充值