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

时间:2020-04-29 21:40:20   收藏:0   阅读:78

技术分享图片

 

 

#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;
}

  

原文:https://www.cnblogs.com/wszhu/p/12804377.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!