zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    Linux 常用命令
    去除重叠区间
    Python 小工具之大文件去重
    有趣的pyfiglet
    杨辉三角
    Paginator分页
    Linux用户和用户组
    Grub介绍
    Linux系统运行级别
    Linux系统启动流程
  • 原文地址:https://www.cnblogs.com/kls123/p/7131495.html
Copyright © 2011-2022 走看看