恳请各位DALAO证鄙人代码时间复杂度
查看原帖
恳请各位DALAO证鄙人代码时间复杂度
1007677
bianyanze楼主2024/10/21 20:00

帮助者互关 本银代码:

#include<bits/stdc++.h>
using namespace std;
string a;
struct node{
	int val;
	int c1,c2;
	node(int n,int m,int k):val(n),c1(m),c2(k){}
	node operator &(const node x){
		if(val==0){
			return node(val&x.val,c1+1,c2);
		}
		return node(val&x.val,c1+x.c1,c2+x.c2);
	}
	node operator |(const node y){
		if(val==1){
			return node(val|y.val,c1,c2+1);
		}
		return node(val|y.val,c1+y.c1,c2+y.c2);
	}
};
int pri(char a){
	if(a=='&')return 2;
	else if(a=='|')return 1;
	else return 0;
}
void cal(stack<node>&s1,stack<char>&s2){
	node a=s1.top();
	s1.pop();
	node b=s1.top();
	s1.pop();
	char op=s2.top();
	s2.pop();
	if(op=='&')s1.push(b&a);
	else s1.push(b|a);
}
stack<node>num;
stack<char>op;
int main(){

	cin>>a;
	for(int i=0;i<a.length();i++){
		if(a[i]>='0'&&a[i]<='1'){
			num.push(node(a[i]-'0',0,0));
		}
		else{
			if(a[i]=='('){
				op.push(a[i]);
				continue;
			}
			if(a[i]==')'){
				while(op.top()!='(')cal(num,op);
				op.pop();
				continue;
			}
			while(!op.empty()&&pri(a[i])<=pri(op.top()))cal(num,op);
			op.push(a[i]);
		}
	}
	while(!op.empty()){
		cal(num,op);
	}
	cout<<num.top().val<<endl<<num.top().c1<<" "<<num.top().c2;
	return 0;
}

thanks a lot~~~

2024/10/21 20:00
加载中...