24分其他全re

P1241 括号序列

Rookie_h @ 2023-02-14 00:32:23

#include <bits/stdc++.h>
using namespace std;
#define is ==
stack<char> st;
stack<char> st2;
int main()
{
    string s;
    char m;
    cin >> s;
    for (int i = 0; i < s.size(); i++)
    {
        if (s[i] is '(' || s[i] is '[')
        {
            st.push(s[i]);
        }
        else if (st.empty() || ((s[i] is ')' && st.top() is '[') || (s[i] is ']' && st.top() is '(')))
        {
            st.push((s[i] == ')') ? '(' : '[');
            st.push(s[i]);
        }
        else
            st.push(s[i]);
    }
    while (!st.empty())
    {
        m = st.top();
        st2.push(m);
        st.pop();
    }
    while (!st2.empty())
    {
        m = st2.top();
        st2.pop();
        if (m is '(')
        {
            cout << m << ')';
        }
        else if (m is '[')
        {
            cout << m << ']';
        }
        else if (m is ')')
        {
            if (st2.top() is '(')
            {
                cout << st2.top() << m;
                st2.pop();
            }
            else
            {
                cout << '(' << m;
            }
        }
        else
        {
            if (st2.top() is '[')
            {
                cout << st2.top() << m;
                st2.pop();
            }
            else
            {
                cout << '[' << m;
            }
        }
    }
}

自己试了几个数据 但是提交就只能过三个


|