半江红求条
查看原帖
半江红求条
1235038
Ekin123楼主2024/10/4 10:32
#include <iostream>
#include <algorithm>
#include <cmath>
#include <stack>
using namespace std;

struct dat
{
	int value,a,o; // a=>skipAND,o=>skipOR
};

stack<dat> num;
stack<char> op;

dat operator &(dat x,dat y)
{
	dat g;
	if(x.value == 0) g = dat{0,x.a + y.a + 1,x.o + y.o};
	else g = dat{x.value && y.value,x.a + y.a,x.o + y.o};
	return g;
}

dat operator |(dat x,dat y)
{
	dat g;
	if(x.value == 1) g = dat{1,x.a + y.a,x.o + y.o + 1};
	else g = dat{x.value || y.value,x.a + y.a,x.o + y.o};
	return g;
}

void deal()
{
	dat y = num.top();
	num.pop();
	dat x = num.top();
	num.pop();
	char c = op.top();
	op.pop();
	if(c == '&') num.push(x & y);
	else if(c == '|') num.push(x | y);
}

int main()
{
	string s;
	cin >> s;
	int n = s.size();
	s = " " + s;
	for(int i = 1;i <= n;i++)
	{
		if(isdigit(s[i]))
		{
			num.push(dat{s[i] - '0',0,0});
		}
		else if(s[i] == '|')
		{
			while(!op.empty() && op.top() != '(')
			{
				deal();
			}
			op.push('|');
		}
		else if(s[i] == '&')
		{
			while(!op.empty() && op.top() == '&')
			{
				deal();
			}
			op.push('&');
		}
		else if(s[i] == '(')
		{
			op.push('(');
		}
		else
		{
			while(!op.empty() && op.top() != '(')
			{
				deal();
			}
			op.pop();
		}
	}
	while(!op.empty()) deal();
	dat ans = num.top();
	cout << ans.value << "\n" << ans.a << " " << ans.o << endl;
	return 0;
} 

https://www.luogu.com.cn/record/179850508

2024/10/4 10:32
加载中...