Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- bool check(char a,char b)
- {
- return (a=='('&&b==')' || a=='{'&&b=='}' || a=='['&&b==']');
- }
- int main()
- {
- int t;
- cin>>t;
- while(t--)
- {
- string s;
- cin>>s;
- bool done=1;
- stack<char>st;
- for(auto u:s)
- {
- if(u=='(' ||u=='{' ||u=='[') st.push(u);
- else
- {
- if(st.empty())
- {
- done=0;
- break;
- }
- else
- {
- if(check(st.top(),u))
- {
- st.pop();
- }
- else
- {
- done=0;
- break;
- }
- }
- }
- }
- if(!st.empty()) done=0;
- if(done==1) cout<<"YES\n";
- else cout<<"NO\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement