// Uva solution:673 - Parentheses Balance
// Uva userid:deepitauiu
// https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=0&problem=614
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
int n,f=0,stacksize=0,c=0;
cin>>n;
while(c<=n){
stack<char> mystack;
c++;
f=0;
getline(cin,s);
for (int i=0;i<s.length();i++)
{
if(s[i]=='('||s[i]=='['){
mystack.push(s[i]);
}
else if(s[i]!=' '){
if (mystack.size()){
char c=mystack.top();
if(s[i]==')'&& c!='('){
f=1;
break;
}
else if(s[i]==']'&& c!='['){
f=1;
break;
}
else{
mystack.pop();
}
}
else{
f=1;
break;
}
}
stacksize=mystack.size();
}
if(c>1){
if((f==0&&stacksize==0)||s.length()==0)
{
cout<<"Yes"<<endl;
}
else
{
cout<<"No"<<endl;
}
}
}
return 0;
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.