P1241后几个样例过不了

P1241 括号序列

cx401845597 @ 2024-07-11 16:00:33

#include <iostream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

char trans(char a)
{
    if (a == '(') return ')';
    if (a == '[') return ']';
}

char tcout(char a)
{
    if (a == '(') return ')';
    if (a == ')') return '(';
    if (a == '[') return ']';
    if (a == ']') return '[';
}

stack<char> s1, s2;
string str;
vector<char> tmp;

int main() {
    cin >> str;
    for (int i = str.length() - 1; i >= 0; i--)
    {
        if (s1.empty()) {
            s1.push(str[i]);
        }
        else {
            if (trans(str[i]) == s1.top()) {
                s2.push(str[i]);
                tmp.push_back(s1.top());
                s1.pop();
            }
            else {
                s1.push(str[i]);
            }
        }
    }
    while (!s1.empty()) {
        if (s1.top() == ')' || s1.top() == ']')
            cout << tcout(s1.top()) << s1.top();
        else cout << s1.top() << tcout(s1.top());
        s1.pop();
    }
    while (!s2.empty()) {
        cout << s2.top();
        s2.pop();
    }
    for (int i = 0; i < tmp.size(); i++)
    {
        cout << tmp[i];
    }

    return 0;
}

感觉整体思路没有问题为什么只有48分,求大佬解答QAQ


|