Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include<bits/stdc++.h>
using namespace std;
int main()
{
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
int n;
scanf("%d",&n);
getchar();
while(n--)
{
string str;
getline(cin,str);
stack<char> p;
bool ok = true;
for(int i=0;i<str.size();++i)
{
if(str[i]=='(')
{
p.push(str[i]);
}
else if(str[i]=='[')
{
p.push(str[i]);
}
else if(str[i]==')')
{
if(!p.empty() && p.top()=='(')
{
p.pop();
}
else
{
ok = false;
break;
}
}
else if(str[i]==']')
{
if(!p.empty() && p.top()=='[')
{
p.pop();
}
else
{
ok = false;
break;
}
}
}
if(!p.empty())
ok = false;
if(ok) puts("Yes");
else puts("No");
}
}
@astinaam

This comment has been minimized.

Copy link
Owner Author

astinaam commented Jul 15, 2016

UVa 673 #stack

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.