文章

23

粉丝

0

获赞

118

访问

3.6k

头像
括号匹配 题解:75通过率原因
P1501 西北工业大学机试题
发布于2026年3月1日 10:47
阅读数 154

当输入字符串以右括号开头时(如")"或"]"),栈为空状态下执行S.top()会导致运行时错误

#include <bits/stdc++.h>
using namespace std;
int main()
{
    stack<int> S;
    string s;
    cin>>s;
    int len=s.length();
    char x;
    for(int i=0; i<len; i++)
    {
        if(s[i]=='(')
            S.push(s[i]);
        if(s[i]=='[')
            S.push(s[i]);
        if(s[i]==')' || s[i]==']')
        {
            x=S.top();
            if((s[i]==')'&&x=='(') || (s[i]==']'&&x=='['))
                S.pop();
            else
      &nbs...

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发