Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- string s;
- bool cancelan (char a, char b)
- {
- if (a=='(' && b==')')
- return true;
- if (a=='[' && b==']')
- return true;
- if (a=='{' && b=='}')
- return true;
- return false;
- }
- bool balanced ()
- {
- string guard;
- guard+=s[0];
- for(int i=1; i<s.size(); i++)
- {
- char ult=guard.back();
- char car=s[i];
- if( cancelan(ult,car) )
- guard.pop_back();
- else
- guard+=car;
- }
- if(guard.size()==0) ///si no me qued0 alg0 por conectar
- return true;
- return false;
- }
- int main()
- {
- int n;
- cin>>n;
- for (int i=0; i<n; i++)
- {
- cin>>s;
- if (balanced())
- cout<<"YES"<<endl;
- else cout<<"NO"<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment