来自44分蒟蒻的求助

P1241 括号序列

laobocai @ 2023-08-23 09:48:52

本蒟蒻求助神犇们,44分怎么改啊?!!

#include<bits/stdc++.h>
using namespace std;
struct id{
    int s;
    char c;
}a;
int main(){
    deque<id> q;
    string s;
    cin>>s;
    for(int i=0;i<s.length();i++){
        a.c=s[i];
        a.s=i;
        if(!q.empty()){
            if((q.back().c=='(' && s[i]==')')||(q.back().c=='[' && s[i]==']'))
                q.pop_back();
            else
                q.push_back(a);
        }
        else
            q.push_back(a);
    }
    for(int i=0;i<s.length();i++){
        if(!q.empty()){
            if(q.front().s==i){
                if(s[i]=='[')
                    cout<<"[]";
                else
                    cout<<"()";
                q.pop_front();
            }
            else
                cout<<s[i];
        }
        else
            cout<<s[i];
    }
    return 0;
}

|