RE求助

P1241 括号序列

_5t0_0r2_ @ 2022-08-03 15:04:27

#include<bits/stdc++.h>
using namespace std;
//À¨ºÅ½á¹¹Ìå
struct kuohao{
    int id;
    char c;
};
stack<kuohao>s;//Õ»ÓÃÀ´ÅжÏ
vector<kuohao>tmp;//°ÑÕû¸ö´®´æÏÂÀ´
vector<kuohao>not_pipei;//´æûÓÐÆ¥ÅäµÄÀ¨ºÅ
//ÅжÏÊÇ·ñÔÚδƥÅäµ±ÖÐ
bool in(int x){
    for(int i = 0;i < (int)not_pipei.size();i++)
        if(not_pipei[i].id == x)
            return true;
    return false;
}
bool pipei(char ch){
    if((s.top().c == '(' && ch == ')') || (s.top().c == '[' && ch == ']') || (s.top().c == '{' && ch == '}'))
        return true;
    return false;
}
int main(){
    char ch_;
    int id = 1;
    //¶ÁÈë
    while(ch_ != '\n'){
        ch_ = getchar();
        tmp.push_back((kuohao){id,ch_});
        if(pipei(ch_))
            s.pop();
        else
            s.push((kuohao){id,ch_});
        id++;
    }
    //Èç¹ûÈ«²¿Æ¥Å䣬ֱ½ÓÊä³ö
    if(s.empty())
        for(int i = 0;i < (int)tmp.size();i++)
            putchar(tmp[i].c);
    else{
        //½«ËùÓÐδƥÅäµÄÀ¨ºÅ¼ÓÈëÊý×éÖÐ
        while(!s.empty()){
            not_pipei.push_back(s.top());
            s.pop();
        }
        //Êä³ö
        for(int i = 0;i < (int)tmp.size();i++){
            char ch = tmp[i].c;
            //Èç¹ûδƥÅ䣬½«ÆäÆ¥Åä
            if(in(i + 1)){
                if(ch == '(' || ch == ')')
                    puts("()");
                else if(ch == '[' || ch == ']')
                    puts("[]");
                else
                    puts("{}");
            }
            else putchar(ch);
        }
    }
    return 0;
}

by a2lyaXNhbWUgbWFyaXNh @ 2022-08-03 15:09:29

食我烫烫烫烫烫

编码炸了


|