一心净土 • 1个月前
using namespace std; stacknum; stackop; stack<pair<int, int>>ans; pair<int, int>c1, c2; int check(char c) {
int a = 0;
if (c == '|')
a = 1;
if (c == '&')
a = 2;
return a;
} void calc() {
int a, b;
char p;
b = num.top();
num.pop();
c2 = ans.top();
ans.pop();
a = num.top();
num.pop();
c1 = ans.top();
ans.pop();
p = op.top();
op.pop();
if (p == '&') {
num.push(a & b);
if (a == 0)
ans.push({c1.first + 1, c1.second});
else
ans.push({c1.first + c2.first, c1.second + c2.second});
}
if (p == '|') {
num.push(a | b);
if (a == 1)
ans.push({c1.first, c1.second + 1});
else
ans.push({c1.first + c2.first, c1.second + c2.second});
}
} void deal() {
string s;
cin >> s;
int d;
for (int i = 0; i < s.size(); i++) {
if (s[i] == '0' || s[i] == '1') {
d = s[i] - '0';
num.push(d);
ans.push({0, 0});
} else if (s[i] == '(' || s[i] == ')') {
if (s[i] == '(')
op.push(s[i]);
else {
while (op.top() != '(')
calc();
op.pop();
}
} else if (s[i] == '&' || s[i] == '|') {
while (op.size() > 0 && check(s[i]) <= check(op.top()))
calc();
op.push(s[i]);
}
}
while (!op.empty()) {
calc();
}
cout << num.top() << endl;
cout << ans.top().first << " " << ans.top().second;
} int main() {
deal();
return 0;
}
评论:
请先登录,才能进行评论