Advertisement
Guest User

Untitled

a guest
Sep 24th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.65 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cstdlib>
  5. #include <string>
  6.  
  7. using namespace std;
  8. struct stack {
  9.     char* m;
  10.     int cap;
  11.     int last;
  12.     stack(){
  13.         cap=8;
  14.         m=(char*)malloc(cap*sizeof(char));
  15.         last=0;
  16.     }
  17.     ~stack(){
  18.         free(m);
  19.     }
  20.     void print(){
  21.         //cout<<cap<<" "<<last<<" $$ ";
  22.         for(int i=0;i<cap;i++)
  23.             if(i>=last) cout<<"x ";
  24.             else cout<<*(m+i)<<" ";
  25.         cout<<'\n';
  26.     }
  27.  
  28.     bool empty(){
  29.        return last==0;
  30.     }
  31.  
  32.     void push(char val) {
  33.         if(last == cap) {
  34.             m=(char*)realloc(m,cap*2*sizeof(char));
  35.             cap*=2;
  36.         }
  37.         m[last++]=val;
  38.     }
  39.  
  40.     int pop(){
  41.         int result = m[--last];
  42.         if(cap > 1 && 4*last<=cap){
  43.             m =(char*)realloc(m,(cap+1)/2*sizeof(char));
  44.             cap=(cap + 1)/2;
  45.         }
  46.         return result;
  47.     }
  48. };
  49. int main(){
  50.     ios::sync_with_stdio(0);
  51.     freopen("bracketsout.txt","w",stdout);
  52.     freopen("bracketsin.txt","r",stdin);
  53.     string s;
  54.     while(cin>>s){
  55.         stack st;
  56.         bool ans=1;
  57.         for(unsigned int i=0;i<s.size();i++){
  58.             if(s[i]=='(' || s[i]=='[')
  59.                 st.push(s[i]);
  60.             if(s[i]==')'){
  61.                 if(st.m[st.last-1]=='(') st.pop();
  62.                    else ans=0;
  63.             }
  64.             if(s[i]==']'){
  65.                 if(st.m[st.last-1]=='[') st.pop();
  66.                     else ans=0;
  67.             }
  68.             //st.print();
  69.         }
  70.         if(st.empty() && ans) cout<<"YES"<<'\n';
  71.         else cout<<"NO"<<'\n';
  72.         //cout<<"test";
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement