对于a&b这种形式,如果是0&,则b的值不重要,发生依次短路,新的额结构体的短路次数=a的短路次数+1
如果是1&,新的结构体的短路次数=a和b的短路次数之和,把新的结构体压入栈中
#include<bits/stdc++.h>
#define N 1000005
using namespace std;
struct Node{
int v, y, h;
};
stack<Node> t;
stack<char> q;
int w[N], k, cnt_y, cnt_h, l, r[N], f[N];
char s[N];
int main(){
scanf("%s",s+1); l = strlen(s+1);
s[0] = '('; s[l+1] = ')';
w['|'] = 1; w['&'] = 2;
for(int i = 0; i <= l+1; i++){
if(s[i] == '('){ //如果是'(',就往右边的栈放
q.push(s[i]);
}
else if(s[i] == '0' || s[i] == '1'){ //如果是数字就往左边的栈放
t.push((Node){s[i] - '0', 0, 0});
}
else{
while(!q.empty() && w[s[i]] <= w[q.top()])
{//如果是符号,且当前优先级小,那就得先算之前的
if(s[i] == ')'){
if(q.top() == '('){
q.pop(); break;
}
}
char z = q.top(); q.pop();
Node a = t.top(); t.pop();
Node b = t.top(); t.pop(); Node c = {0,0,0};
// cout<<i<<" "<<endl;
c.y=b.y; c.h=b.h;
if(z == '&'){
if(b.v == 0) c.y++;
else c.y+=a.y,c.h+=a.h;
c.v = a.v & b.v;
}
else if(z == '|'){
if(b.v == 1) c.h++;
else c.h+=a.h,c.y+=a.y;
c.v = b.v | a.v;
}
t.push(c);
}
if(s[i] != ')') q.push(s[i]);
}
}
// cout<<t.size()<<endl;
printf("%d \n%d %d", t.top().v, t.top().y, t.top().h);
return 0;
}
标签:逻辑,int,短路,cnt,stack,次数,CSP,表达式
From: https://www.cnblogs.com/caterpillor/p/16891771.html