Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- stack <char> s;
- string c;
- int main()
- {
- int n,ok;
- scanf("%d",&n);
- while (n--)
- {
- ok=1;
- while (!s.empty())
- s.pop();
- cin >> c;
- for (int i=0;i<c.size();i++)
- {
- if (c[i]=='(' || c[i]=='[')
- s.push(c[i]);
- else if ((s.size()!=0 && s.top()=='('&& c[i]==')') || (s.size()!=0 && s.top()=='['&& c[i]==']'))
- s.pop();
- else if (s.size()==0 && c[i]==')' || s.size()==0 && c[i]==']')
- ok=0;
- else if ((s.size()!=0 && s.top()=='(' && c[i]==']')|| (s.size()!=0 && s.top()=='['&& c[i]==')'))
- ok=0;
- }
- if (s.size())
- ok=0;
- if (ok)
- printf("Yes\n");
- else
- printf("No\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement