表达式树;70pts求hack;2关注
查看原帖
表达式树;70pts求hack;2关注
354482
Ben幻影楼主2024/8/4 01:35

这个hack过了

record

#include <bits/stdc++.h>
using namespace std;
bool isnum(char x){
	return x>='0'&&x<='9';
}
map<int,char> m;
string mtob(string a){
	m['&']=2,m['|']=1;
	string ans="";
	a='('+a+')';
	stack<char> s;
	int l=a.length();
	for(int i=0;i<l;i++){
		if(isnum(a[i])) ans+=a[i];
		else{
			if(a[i]=='(') s.push('(');
			else if(a[i]==')'){
				while(s.top()!='(') ans+=s.top(),s.pop();
				s.pop();
			}
			else if(s.empty()) s.push(a[i]);
			else if(m[a[i]]>m[s.top()]||s.top()=='(') s.push(a[i]);
			else{
				while((!m[s.top()]<m[a[i]])&&s.top()!='(') ans+=s.top(),s.pop();
				s.push(a[i]);
			}
		}
	} 
	while(!s.empty()) ans+=s.top(),s.pop();
	return ans;
}
struct node{
	char n;int l,r,f;
}tree[2000000];
node mnode(char a,int l,int r,int b){
	node x;x.n=a,x.l=l,x.r=r,x.f=b;
	return x; 
} 
int maketree(string a){
	int l=a.length();
	stack<node> n; 
	for(int i=0;i<l;i++){
		tree[i]=mnode(a[i],-1,-1,i);
	}
	for(int i=0;i<l;i++){
		node x,y;
		if(a[i]=='|'||a[i]=='&'){
			x=n.top();n.pop();
			y=n.top();n.pop();
			tree[x.f].f=i;
			tree[y.f].f=i;
			tree[i].l=y.f;
			tree[i].r=x.f;
			n.push(tree[i]);
		}else n.push(tree[i]);
	} 
	return (n.top()).f;
}
int flag1=0,flag2=0;
int dfs(int s){
	if(tree[s].n=='|'){
		int x=dfs(tree[s].l);
		if(x==1){
			flag1++;
			return 1; 
		}else{
			int y=dfs(tree[s].r);
			tree[s].n=((x|y)+'0');
			return (x|y);
		}
	}else if(tree[s].n=='&'){
		int x=dfs(tree[s].l);
		if(x==0){
			flag2++;
			return 0; 
		}else{
			int y=dfs(tree[s].r);
			tree[s].n=((x&y)+'0');
			return (x&y);
		}
	}else{
		return int(tree[s].n-'0');
	}
} 
int main(){
	string a;cin>>a;
	string b=mtob(a);
	int top=maketree(b);
	cout<<dfs(top)<<endl;
	cout<<flag2<<" "<<flag1<<endl;
	return 0;
}
2024/8/4 01:35
加载中...