求助,50分
查看原帖
求助,50分
665095
Rank114514楼主2022/11/6 15:01

50分

#include <iostream>
#include <cstdio>
#include <stack>
#include <algorithm>
using namespace std;
string s; int x,y;
stack<bool> sv; stack<int> so;
void calc()
{
	bool a,b;
	int op=so.top();
	so.pop();
	if(op==1)
	{
		a=sv.top();
		sv.pop();
		b=sv.top();
		sv.top();
		sv.push(a|b);
	}
	if(op==2)
	{
		a=sv.top();
		sv.pop();
		b=sv.top();
		sv.top();
		sv.push(a&b);
	}
}
int fun(int p)
{
	if(s[p]=='0'||s[p]=='1')
	{
		return p;
	}
	if(s[p]=='(')
	{
		stack<char> sc;
		for(int j=p; ; j++)
		{
			if(s[j]=='(')
			{
				sc.push('(');
			}
			if(s[j]==')')
			{
				if(sc.size()==1)
				{
					sc.pop();
					return j;
				}
				else
				{
					sc.pop();
				}
			}
		}
	}
}
int main(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	cin>>s;
	s='('+s+')';
	for(int i=0; i<s.size(); i++)
	{
		if(s[i]=='0')
		{
			sv.push(false);
		}
		else if(s[i]=='1')
		{
			sv.push(true);
		}
		else if(s[i]=='(')
		{
			so.push(-1);
		}
		else if(s[i]=='|')
		{
			while(!so.empty()&&so.top()>=1)
			{
				calc();
			}
			so.push(1);
			if(sv.top()==true)
			{
				y++;
				int z=i;
				
				while(1)
				{
					z=fun(z+1);
					if(s[z+1]==')'||s[z+1]=='|')
					{
						break;
					}
					else
					{
						z++;
					}
				}
				
				i=z;
				so.pop();
			}
		}
		else if(s[i]=='&')
		{
			while(!so.empty()&&so.top()>=2)
			{
				calc();
			}
			so.push(2);
			if(sv.top()==false)
			{
				x++;
				i=fun(i+1);
				so.pop();
			}
		}
		else if(s[i]==')')
		{
			while(!so.empty()&&so.top()>-1)
			{
				calc();
			}
			so.pop();
		}
	}
	cout<<sv.top()<<endl<<x<<" "<<y;
	return 0;
}


2022/11/6 15:01
加载中...