-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbracket_sequence.cpp
More file actions
78 lines (76 loc) · 1.61 KB
/
bracket_sequence.cpp
File metadata and controls
78 lines (76 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <bits/stdc++.h>
using namespace std;
void check(string exp)
{
stack<char> s;
for(long long int i=0;i<exp.length();i++)
{
if(exp[i]=='('||exp[i]=='{'||exp[i]=='['||exp[i]=='<')
{
s.push(exp[i]);
continue;
}
if(exp[i]=='|')
{
if(!s.empty()&&s.top()=='|')
{
s.pop();
}
else
{
s.push(exp[i]);
}
continue;
}
if(s.empty())
{
cout<<"NO"<<endl;
return ;
}
char x=s.top();
s.pop();
int d=0;
switch(exp[i])
{
case ')':
if(x!='(')
{
d=1;
}
break;
case '}':
if(x!='{')
d=1;
break;
case ']':
if(x!='[')
d=1;
break;
case '>':
if(x!='<')
d=1;
}
if(d==1)
{
cout<<"NO"<<endl;
return ;
}
}
if(!s.empty())
cout<<"NO"<<endl;
else
cout<<"YES"<<endl;
return ;
}
int main()
{
long long int t;
cin>>t;
string exp;
while(t--)
{
cin>>exp;
check(exp);
}
return 0;
}