forked from Krish03na/CS204
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab_3.cpp
72 lines (54 loc) · 1019 Bytes
/
lab_3.cpp
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
#include <bits/stdc++.h>
using namespace std;
bool balanced(string a){
stack<char> s;
char x;
for(int i=0; i<a.length(); i++){
if(a[i]=='(' || a[i]=='{' || a[i]=='[' || a[i]=='<' ){
s.push(a[i]);
continue;
}
if(a[i] == '|'){
if(s.empty()) s.push(a[i]);
else{if(s.top()=='|') s.pop();
else s.push(a[i]);}
continue;
}
if(s.empty()) return false;
switch(a[i]){
case ')': x = s.top();
s.pop();
if(x=='{' || x=='[' || x=='<' || x=='|' )
return false;
break;
case '}': x = s.top();
s.pop();
if(x=='(' || x=='[' || x=='<' || x=='|' )
return false;
break;
case ']': x = s.top();
s.pop();
if(x=='(' || x=='{' || x=='<' || x=='|' )
return false;
break;
case '>': if(s.empty()) return false;
x = s.top();
s.pop();
if(x=='(' || x=='[' || x=='{' || x=='|' )
return false;
break;
}
}
return (s.empty());
}
int main(){
int T;
cin >> T;
while(T--){
string str;
cin >> str;
if(balanced(str)) cout << "YES\n";
else cout << "NO\n";
}
return 0;
}