-
Notifications
You must be signed in to change notification settings - Fork 0
/
1106.cpp
36 lines (36 loc) · 1.18 KB
/
1106.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
class Solution {
public:
bool parseBoolExpr(string expression) {
stack<char> data;
stack<char> op;
for(char c : expression){
if(c=='t'||c=='f'||c=='(') data.push(c);
else if(c=='!'||c=='|'||c=='&') op.push(c);
else if(c==')'){
char o = op.top();op.pop();
if(o=='!'){
char tmp = data.top();data.pop();data.pop();
if(tmp=='t') data.push('f');
else data.push('t');
}else if(o=='|'){
char res = 'f';
char tmp = 'a';
while(tmp!='('){
tmp = data.top();data.pop();
if(tmp=='t') res = 't';
}
data.push(res);
}else{
char res = 't';
char tmp = 'a';
while(tmp!='('){
tmp = data.top();data.pop();
if(tmp=='f') res = 'f';
}
data.push(res);
}
}else continue;
}
return data.top()=='t';
}
};