Educational Codeforces Round 4 C. Replace To Make Regular Bracket Sequence

题目链接:http://codeforces.com/contest/612/problem/C

解题思路:

题意就是要求判断这个序列是否为RBS,每个开都要有一个和它对应的关,如:<()>满足条件,但<(>)就不满足条件,反正直接就是用栈做就行了,完美符合题目要求。

#include <bits/stdc++.h>
using namespace std;
stack<char>st;
/*struct node{
    int num,id;
}a[200009];
bool cmp(const node x,const node y){
    return x.num<y.num;
}*/
int main()
{
    string s;
    cin>>s;
    int ans =0,i;
    for(i=0;i<s.size();i++){
        if(s[i] == ']'){
            if(st.size() == 0)  return puts("Impossible");
            if(st.top() == '[')
                st.pop();
            else{
                ans++; st.pop();
            }
        }
        else if(s[i]=='>'){
            if(st.size() == 0)  return puts("Impossible");
            if(st.top() == '<')
                st.pop();
            else{
                ans++; st.pop();
            }
        }
        else if(s[i]=='}'){
            if(st.size() == 0)  return puts("Impossible");
            if(st.top() == '{')
                st.pop();
            else{
                ans++; st.pop();
            }
        }
        else if(s[i]==')'){
            if(st.size() == 0)  return puts("Impossible");
            if(st.top() == '(')
                st.pop();
            else{
                ans++; st.pop();
            }
        }
        else
            st.push(s[i]);
    }
    if(st.size()==0)
    cout<<ans<<endl;
    else
    cout<<"Impossible"<<endl;
}
原文地址:https://www.cnblogs.com/kls123/p/7131495.html