40pts RE求调
查看原帖
40pts RE求调
866315
_February20th_楼主2023/11/22 22:57
#include <bits/stdc++.h>
#define int long long

using namespace std;

string s;
stack<int> ns;
stack<char> fs;
stack<pair<int,int>> ds;
map<char,int> pri;

void calc() {
	int b=ns.top();
	ns.pop();
	int a=ns.top();
	ns.pop();
	auto db=ds.top();
	ds.pop();
	auto da=ds.top();
	ds.pop();
	int tmp=0;
	if(fs.top()=='&') tmp=a&b;
	else if(fs.top()=='|') tmp=a|b;
	ns.push(tmp);
	if(a==0&&fs.top()=='&') ds.push({da.first+1,da.second});
	else if(a==1&&fs.top()=='|') ds.push({da.first,db.second+1});
	else ds.push({da.first+db.first,da.second+db.second});
	fs.pop();
	return ;
}

signed main() {
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin>>s;
	pri['|']=0;
	pri['&']=1;
	for(int i=0; i<s.size(); i++) {
		if(isdigit(s[i])) {
//			cout<<"i="<<i;
			ns.push(s[i]-'0');
			ds.push({0,0});
//			cout<<"Line 40 OK!"<<endl;
		} else {
			if(s[i]=='(') {
				cout<<"i="<<i;
				fs.push('(');
//				cout<<"Line 45 OK!"<<endl;
			}
			if(s[i]==')') {
//				cout<<"i="<<i;
				while(!fs.empty()&&fs.top()!='(') calc();
				if(!fs.empty()) fs.pop();
//				cout<<"Line 51 OK!"<<'\n';
			}
			if(s[i]!='('&&s[i]!=')') {
//				cout<<"i="<<i;
				while(!fs.empty()&&pri[fs.top()]>=pri[s[i]]) calc();
				fs.push(s[i]);
//				cout<<"Line 57 OK!"<<endl;
			}
		}
	}
	while(!fs.empty()) calc();
	cout<<ns.top()<<'\n'<<ds.top().first<<' '<<ds.top().second<<'\n';
	return 0;
}
2023/11/22 22:57
加载中...