Balanced Brackets

Problem Link

  • References

 

Shafaet’s Planet

<Solution>

#include<bits/stdc++.h>
#include<string>

#include<stdio.h>
#define fr(i,a,b) for(i=a;i<b;i++)
using namespace std;
int main()
{
int t,n,i,j;
string sr;
cin>>n;
cin.ignore();
for(i=0;i<n;i++)
{
stack<char>s;
getline(cin,sr);
fr(j,0,sr.size())
{if(!s.empty() &&sr[j]==’)’&&s.top()=='(‘)
s.pop();
else if(!s.empty() &&sr[j]==’]’&&s.top()=='[‘)
s.pop();
else if(!s.empty() &&sr[j]==’}’&&s.top()=='{‘)
s.pop();

else s.push(sr[j]);
}
printf(“%s\n”,!s.empty()?”NO”:”YES”);
}

}

Advertisements

About waprogramming

I am in CSE,from CUET
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s