题目链接
Solution:
其实这一题没有什么好说的全靠看代码理解
我们可以分两步来解决
首先看到表达式求值,我们最先想到的肯定是栈
我们这里就是先建立起表达式树
然后用dfs遍历表达式树,进行求值,求短路次数
code:
#include <bits/stdc++.h>
using namespace std;
struct node{
int x,y;
};
string s;
int optp[10086];
stack<char> opt;
stack<int> num;
char stk[1000005];
int top,ans,ans2,ans3;
node optree[1000005];
int dfs(int step){
if (stk[step]=='0') return 0;
if (stk[step]=='1') return 1;
if (stk[step]=='&'){
if (dfs(optree[step].x)==0){
ans2++;
return 0;
}
return dfs(optree[step].y);
}
else{
if (dfs(optree[step].x)==1){
ans3++;
return 1;
}
retu