32分代码求调

P1241 括号序列

__why @ 2023-12-15 20:12:45

啊啊啊?为啥调完后从44分到32分了QAQ

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e3+5;
int top = 0,a[maxn];
void check(){
    if(top!=0){
        if(a[top-1]==1)cout<<')';
        else cout<<']';
        top--;
    }
}
int main(){
    char c;
    while(cin>>c){
        if(c=='('){
            check();
            a[top++] = 1;
            cout<<c;
        }else if(c==')'){
            if(a[top-1]==2){
                cout<<']';
                top--;
            }
            if(a[top-1]!=1){
                cout<<'(';
            }else{
                top--;
            }
            cout<<c;
        }else if(c=='['){
            check();
            if(top!=0){
                if(a[top-1]==1)cout<<')';
                else cout<<']';
                top--;
            }
            a[top++] = 2;
            cout<<c;
        }else{
            if(a[top-1]==1){
                cout<<')';
                top--;
            }
            if(a[top-1]!=2){
                cout<<'[';
            }else{
                top--;
            }
            cout<<c;
        }
    }

}

44分:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e3+5;
int a[maxn];
int main(){
    char c;
    int top = 0;
    while(cin>>c){
        if(c=='('){
            a[top++] = 1;
            cout<<c;
        }else if(c==')'){
            if(a[top-1]!=1){
                cout<<'(';
            }else{
                top--;
            }
            cout<<c;
        }else if(c=='['){
            a[top++] = 2;
            cout<<c;
        }else{
            if(a[top-1]!=2){
                cout<<'[';
            }else{
                top--;
            }
            cout<<c;
        }
    }
}

by wazrf1314 @ 2023-12-15 20:49:49

那道题?


|