2020CSP-J普及组第二轮试题及解析(第三题表达式expr)

参考程序代码:

#include <bits/stdc++.h>
using namespace std;

const int N = 1000005; //树的节点比变量数多,但肯定不超过10^6
string str;
int n, q, cnt;
vector<int> tree[N]; 
char o[N]; //树中每个节点对应的操作符,如果有
int v[N]; //树中每个节点的值
stack<int> stk;
bool flag[N]; //标记某个变量的值的变化是否影响最终的结果

//计算表达式的值
int dfs1(int root)
{
    if(root <= n) return v[root]; //变量叶子节点直接返回变量值
    if(o[root] == '&') return v[root] = dfs1(tree[root][0]) & dfs1(tree[root][1]);
    if(o[root] == '|') return v[root] = dfs1(tree[root][0]) | dfs1(tree[root][1]);
    if(o[root] == '!') return v[root] = !dfs1(tree[root][0]);
}

//标记所有节点的变化会影响表达式的结果
void dfs2(int root)
{
    flag[root] = true;
    if(o[root] == '&')
    {
        if(v[tree[root][0]] == 1) dfs2(tree[root][1]); //如果左子树值是1,则标记右子树的值会影响表达式结果
        if(v[tree[root][1]] == 1) dfs2(tree[root][0]); //如果右子树值是1,则标记左子树的值会影响表达式结果
    }
    if(o[root] == '|')
    {
        if(v[tree[root][0]] == 0) dfs2(tree[root][1]); //如果左子树结果是0,则标记右子树的值会影响表达式结果
        if(v[tree[root][1]] == 0) dfs2(tree[root][0]); //如果右子树结果是0,则标记左子树的值会影响表达式结果
    }
    if(o[root] == '!') dfs2(tree[root][0]); //子树的值肯定影响结果
}

int main()
{
    getline(cin, str);
    cin >> n;
    for(int i = 1; i <= n; i++) cin >> v[i]; //所有变量初始值

    cnt = n; //节点编号跳过变量编号
    for(int i = 0; i < str.length(); i++)
    {
        if(str[i] == 'x')
        {
            int num = 0;
            int j = i + 1; //读变量x后面的数字
            while(str[j] >= '0' && str[j] <= '9')
            {
                num = num * 10 + str[j] - '0';
                j++;
            }
            stk.push(num); //变量编号入栈
            i = j - 1;
        }
        else if(str[i] == '&' || str[i] == '|')
        {
            int a = stk.top(); stk.pop();
            int b = stk.top(); stk.pop();
            cnt++; //符号节点累加
            stk.push(cnt); //符号节点编号入栈
            tree[cnt].push_back(a); //节点cnt和a之间建立一条边
            tree[cnt].push_back(b); //节点cnt和b之间建立一条边
            o[cnt] = str[i];
        }
        else if(str[i] == '!')
        {
            int a = stk.top(); stk.pop();
            cnt++;
            stk.push(cnt);
            tree[cnt].push_back(a);
            o[cnt] = '!';
        }
    }
    int res = dfs1(stk.top());
    dfs2(stk.top());

    int t;
    cin >> q;
    while(q--)
    {
        cin >> t;
        if(flag[t]) cout << !res << endl;
        else cout << res << endl;
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值