52分代码求调

P1241 括号序列

wangyutian578 @ 2023-08-12 15:48:11

#include <bits/stdc++.h>
#define MX 100
using namespace std;
int vis[MX + 10];
int main()
{
    string s;
    stack<char> p;
    stack<int> q;
    cin >> s;
    int len = s.size(),i;
    for(i = 0;i < len;i++)
    {
        if(s[i] == '[' || s[i] == '(')
        {
            p.push(s[i]);
            q.push(i);
        }
        else if(s[i] == ')' && p.top() == '(')
        {
            p.pop();
            vis[q.top()] = 1;
            vis[i] = 1;
            q.pop();
        }
        else if(s[i] == ']' && p.top() == '[')
        {
            p.pop();
            vis[q.top()] = 1;
            vis[i] = 1;
            q.pop();
        }
    }
    for(i = 0;i < len;i++)
    {
        if(vis[i] == 1)
        {
            cout << s[i];
        }
        else
        {
            if(s[i] == '(')
            {
                cout << s[i] << ')';
            }
            else if(s[i] == '[')
            {
                cout << s[i] << ']';
            }
            else if(s[i] == ')')
            {
                cout << '(' << s[i];
            }
            else
            {
                cout << '[' << s[i];
            }
        }
    }
    cout << endl;
    return 0;
}

by wangyutian578 @ 2023-08-12 15:49:01

https://www.luogu.com.cn/record/120500155

提交记录


by wangyutian578 @ 2023-08-12 15:49:34

RE是怎么回事???


by wangyutian578 @ 2023-08-12 15:58:24

唔,有人吗?


by zhoujk @ 2023-08-15 15:07:52

@yinzongrui 不知道楼主解决问题了没有。 运行错误可能是因为没有判断栈是否为空,在栈空的时候还读取栈顶数据或者执行出栈操作都会发生运行错误。


by wangyutian578 @ 2023-08-15 18:24:04

@zhoujk 哦,谢谢


|