68求调 #8#9#10#11 WA

P1241 括号序列

H2O2_ @ 2024-07-23 14:41:39

8 #9 #10 #11 WA

(是的又是窝

#include<bits/stdc++.h>
using namespace std;
int num,a,b[100];
stack<char> s; 
char pd(char a){
    if(a==')')return '(';
    if(a==']')return '[';
    return '\0';
} 
int main(){
    string p;
    cin>>p;
    for(int i=0;i<p.size();i++){
        if(s.empty()){
            s.push(p[i]);
            continue;
        }
        if(pd(p[i])==s.top()){
            s.pop();
        }
        else s.push(p[i]);

    }
    if(s.empty())cout<<p;
    else{
        for(int i=0;i<p.size();i++){
            if((p[i]=='('&&p[i+1]!=')')||(p[i]==')'&&p[i-1]!='(')){
                cout<<"()";
            }
            else if((p[i]=='['&&p[i+1]!=']')||(p[i]==']'&&p[i-1]!='[')){
                cout<<"[]";
            }
            else cout<<p[i];
        } 
    } 
 return 0; }

|