AlenAntonelli

Balanced Brackets {[(una string)]} HD

Jun 3rd, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. string s;
  7.  
  8. bool cancelan (char a, char b)
  9. {
  10.     if (a=='(' && b==')')
  11.         return true;
  12.     if (a=='[' && b==']')
  13.         return true;
  14.     if (a=='{' && b=='}')
  15.         return true;
  16.     return false;
  17. }
  18.  
  19. bool balanced ()
  20. {
  21.     string guard;
  22.     guard+=s[0];
  23.    
  24.     for(int i=1; i<s.size(); i++)
  25.     {
  26.         char ult=guard.back();
  27.         char car=s[i];
  28.         if( cancelan(ult,car) )
  29.             guard.pop_back();
  30.         else
  31.             guard+=car;
  32.     }
  33.    
  34.     if(guard.size()==0) ///si no me qued0 alg0 por conectar
  35.         return true;
  36.     return false;
  37. }
  38.  
  39. int main()
  40. {
  41.     int n;
  42.     cin>>n;
  43.     for (int i=0; i<n; i++)
  44.     {
  45.         cin>>s;
  46.         if (balanced())
  47.             cout<<"YES"<<endl;
  48.         else cout<<"NO"<<endl;
  49.     }
  50.  
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment