【小米OJ-括号配对】栈的使用

 

 

#include <bits/stdc++.h>

using namespace std;

int main()
{
    // please write your code here
    stack<int>st;
    string str;
    while(cin>>str){
        while(!st.empty()) st.pop();
        for(int i=0;i<str.size();i++){
          if(st.empty()) st.push(str[i]);
          else if(st.top()=='['&&str[i]==']') st.pop();
          else if(st.top()=='('&&str[i]==')') st.pop();
          else if(st.top()=='{'&&str[i]=='}') st.pop();
          else st.push(str[i]);
        }
        if(st.empty()) cout<<1<<endl;
        else cout<<0<<endl;
    }
    return 0;
}

  

posted @ 2020-04-29 20:17  wusheng_z  阅读(184)  评论(0编辑  收藏  举报