萌新,刚学OI,70分求助

P1241 括号序列

NGC224 @ 2019-11-11 16:15:59

#include <iostream>
#include <cstdio>
#include <string>
#include <stack>
using namespace std;
string s;
stack<pair<char,int> > q;
int book[205];
int main()
{
    cin>>s;
//  for(int i=0;i<s.size();i++)
//      cout<<s[i]<<" ";
    for(int i=0;i<s.length();i++)
    {
        if(s[i]==')' && !q.empty() && q.top().first=='(')
        {
            book[q.top().second]=0;
            q.pop();
            continue;
        }
        if(s[i]==']'&& !q.empty() && q.top().first=='[')
        {
            book[q.top().second]=0;
            q.pop();
            continue;
        }
        book[i]=1;
        q.push(make_pair(s[i],i));
    }
    for(int i=0;i<s.length();i++)
    {
        if(book[i])
        {
            if(s[i]==')' || s[i]=='(')
            {
                cout<<"("<<")";
                continue;
            }
            if(s[i]=='[' || s[i]==']')
            {
                cout<<"["<<"]";
                continue;
            }
        }
        cout<<s[i];
    }

    return 0;
}

by ChlorinumStars @ 2020-04-19 21:10:34

我也是70分,我的问题是只用了一个栈不能处理(((【)))的问题


|