32pts 求调

P1241 括号序列

jqQt0220 @ 2023-10-03 19:11:38

rt,记录

#include<bits/stdc++.h>
using namespace std;
stack<int> s;
int f[1010];
string c;
int main()
{
    cin>>c;
    int l=c.length();
    for(int i=0;i<l;i++)
    {
        if(c[i]==')'||c[i]==']')
        {
            while(!s.empty()&&c[s.top()]!='('&&c[s.top()]!='[')
                s.pop();
            if(c[i]==')'&&c[s.top()]=='('||c[i]==']'&&c[s.top()]=='[')
                f[i]=f[s.top()]=1;
        }
        else
        {
            s.push(i);
        }
    }
    for(int i=0;i<l;i++)
    {
        if(f[i])
            printf("%c",c[i]);
        else
        {
            if(c[i]=='('||c[i]==')')
                printf("()");
            if(c[i]=='['||c[i]==']')
                printf("[]");
        }
    }
    return 0;
}

by 帝都_henry26268 @ 2023-10-04 09:13:04

提供一个AC代码

#include<iostream>
#include<stack>
#include<queue>
using namespace std;

const int MAXS = 1e3+10;

int n;

string str;

stack<char> st;
queue<char> q;

int pos[MAXS];

int main(){

    cin >> str;

    int cnt = 0,tp = 0;
    for(int i = 0;i < str.size();i++){

        if(str[i] == '(' || str[i]== '['){
            st.push(str[i]);
            pos[++cnt] = 1;
            tp = cnt;
        }else{
            bool flag = false;

            if(st.empty())
                flag = true;
            else if( (st.top() == '(' && str[i] == ']') || (st.top() == '[' && str[i] == ')') )
                flag = true;
            else if( (st.top() == '(' && str[i] == ')') || (st.top() == '[' && str[i] == ']') ){
                st.pop();
                while(pos[tp] == 0)tp--; 
                pos[tp] = 0;
            }

            if(flag){
                if(str[i] == ')')pos[++cnt] = 0,q.push('(');
                if(str[i] == ']')pos[++cnt] = 0,q.push('[');
            }
        }
        q.push(str[i]);
    }

    int id = 0;
    while(!q.empty()){
        cout << q.front();
        int f = q.front();

        if(f == '('){
            id++;
            if(pos[id] == 1)cout << ')';
        }
        else if(f == '['){
            id++;
            if(pos[id] == 1)cout << ']';
        }
        q.pop();

    }
    return 0;
}

以及一个关于你代码debug的提示:

使用st.top()这一类函数时应当先判断是否为空


by NaCl_0_9H2O @ 2023-10-04 10:54:00

@帝都_henry26268 楼上好长!(滑稽)

#include<bits/stdc++.h>
using namespace std;
string s;
int ss[101];
int main(){
    cin>>s;
    int a=s.size();
    for(int i=0;i<a;i++){
        if(s[i]==')'){
            for(int j=i;j>=0;j--){
                if(ss[j]==0){
                    if(s[j]=='('){
                        ss[i]=ss[j]=1;
                        break;
                    }
                    else if(s[j]=='[') break;
                }
            }
        }
        else if(s[i]==']'){
            for(int j=i;j>=0;j--){
                if(ss[j]==0){
                    if(s[j]=='['){
                        ss[i]=ss[j]=1;
                        break;
                    }
                    else if(s[j]=='(') break;
                }
            }
        }
    }
    for(int i=0;i<a;i++){
        if(ss[i]==0){
            if(s[i]=='('||s[i]==')')cout<<"()";
            else cout<<"[]";
        }
        else cout<<s[i];
    }
    return 0;
}

AC 2.0


by 帝都_henry26268 @ 2023-10-04 11:02:10

@XO_GODG 自认代码码风比较清晰,但确实冗长


by NaCl_0_9H2O @ 2023-10-04 11:04:40

长非长,娼常长


|