68pts玄关求条

P1241 括号序列

H_dream @ 2024-11-15 20:50:16

#include<bits/stdc++.h>
#define int long long
using namespace std;
string s;
stack<char> s1;
stack<int> s2;
bool a[110];
signed main(){
    cin>>s;
    for(int i=0;i<s.size();++i){
        if(s[i]=='('||s[i]=='['){
            s1.push(s[i]);
            s2.push(i);
        }
        else {
            if(s1.empty()){
                a[i]=1;
                continue;
            }
            char x=s1.top();
            int y=s2.top();
            s1.pop(); s2.pop();
            if(s[i]==')'&&x!='('||s[i]==']'&&x!='['){
                a[i]=a[y]=1;
            }
        }
    }
    while(!s2.empty()){
        a[s2.top()]=1;
        s2.pop();
    }
    for(int i=0;i<s.size();++i){
        if(a[i]){
            if(s[i]=='('||s[i]==')') cout<<"()";
            if(s[i]=='['||s[i]==']') cout<<"[]";
        }
        else cout<<s[i];
    }
    return 0;
}

|