dalao的警示
查看原帖
dalao的警示
671208
control_our_own_life楼主2022/11/5 09:30
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <stack>
using namespace std;

struct node {
	int data, y, h;
};
int a[130];
string s;
stack <char> op;
stack<node> ans;

void calc() {
	node rc = ans.top();
	ans.pop();
	node lc = ans.top();
	ans.pop();
	char ch = op.top();
	op.pop();
	node rt;
	rt.y = lc.y, rt.h = lc.h;
	if (ch == '&') {
		if (!lc.data)
			rt.y++, rt.data = 0;
		else
			rt.data = rc.data, rt.y += rc.y, rt.h += rc.h;
	} else {
		if (lc.data)
			rt.h++, rt.data = 1;
		else
			rt.data = rc.data, rt.y += rc.y, rt.h += rc.h;
	}
	ans.push(rt);
}

int main() {
	a['('] = 0, a['|'] = 1, a['&'] = 2;
	cin >> s;
	for (int i = 0; s[i]; i++) {
		if (s[i] == '0' || s[i] == '1')
			ans.push({s[i] - '0', 0, 0});
		else if (s[i] == '(')
			op.push(s[i]);
		else if (s[i] == ')') {
			while (op.top() != '(')
				calc();
			op.pop();
		} else {
			while (!op.empty() && a[s[i]] <= a[op.top()])
				calc();
			op.push(s[i]);
		}
	}
	while (!op.empty())
		calc();
	printf("%d\n", ans.top().data);
	printf("%d %d", ans.top().y, ans.top().h);
	return 0;
}/*
0&(1|0)|(1|1|1&0)
answer:
1
1 2
*/

2022/11/5 09:30
加载中...