56分!!!

P1241 括号序列

18607316034x @ 2024-07-23 15:02:57

#include<bits/stdc++.h>

using namespace std;

string s;

int a[110],c;

char b[350];

int main(){

    cin>>s;
    for(int i=s.size()-1;i>=0;i--){
        int num=0,tmp=0;
        while(s[i]==')'||s[i]==']'){
            i--;
            num++;
        }
        while(num!=0){
            num--;
            tmp++;
            i--;
            if((s[i+1]=='('&&s[i+1+tmp]==')')||(s[i+1]=='['&&s[i+1+tmp]==']')){
                a[i+1]=1;
                a[i+1+tmp]=1;
            }
            else{
                i++;
                break;
            } 
            tmp++;
        }
    /*  if(s[i]==')'||s[i]==']'){
            int j=i;
            while(s[j]!='('&&s[j]!='['&&j!=0){
                j--;
            }
            if(a[j]!=1){
                if(s[i]==')'&&s[j]=='('){
                    a[i]=1;
                    a[j]=1;
                }
                if(s[i]==']'&&s[j]=='['){
                    a[i]=1;
                    a[j]=1;
                }
            }

        }*/
    }
    for(int i=0;i<s.size();i++){
        if(a[i]==0){
            if(s[i]=='['){
                b[c]=s[i];
                c++;
                b[c]=']';
                c++;
            }
            if(s[i]==']'){
                b[c]='[';
                c++;
                b[c]=s[i];
                c++;
            }
            if(s[i]=='('){
                b[c]=s[i];
                c++;
                b[c]=')';
                c++;
            }
            if(s[i]==')'){
                b[c]='(';
                c++;
                b[c]=s[i];
                c++;
            }
        }
        else{
            b[c]=s[i];
            c++;
        }
    }
    for(int i=0;i<c;i++){
        cout<<b[i];
    }
}

by haimingbei @ 2024-07-23 15:09:50

为啥都在做这题?


|