1908 · 布尔表达式求值(stack单调栈&dfs)

链接:

LintCode 炼码 - 更高效的学习体验!

题解:

1.按照优先级构造表达式树,true/false(INT_MAX) > and(2) > or(1)d

2.然后后序and or求值,可以做短路求值

#include <string>
#include <vector>
#include <stack>
#include <sstream>
#include <climits>

using namespace std;

class ExpTreeNode {
public:
    string symbol;
    ExpTreeNode *left, *right;
    int priority;  // 用于构建表达式树时的优先级比较

    ExpTreeNode(string symbol) {
        this->symbol = symbol;
        this->left = this->right = nullptr;
        this->priority = 0;  // 默认值
    }
};

class Solution {
public:
    /**
     * @param expression: a string that representing an expression
     * @return: the result of the expression
     */
    string evaluation(string &expression) {
        // 1. 分词
        vector<string> tokens;
        stringstream ss(expression);
        string token;
        while (ss >> token) {
            tokens.push_back(token);
        }
        if (tokens.empty()) return "error";

        // 2. 语法检查(仅检查 token 合法性及顺序,不构建树)
        bool expectOperand = true;  // 当前是否期望操作数
        for (const string &t : tokens) {
            if (t == "true" || t == "false") {
                if (!expectOperand) return "error";
                expectOperand = false;
            } else if (t == "and" || t == "or") {
                if (expectOperand) return "error";
                expectOperand = true;
            } else {
                return "error";  // 非法 token
            }
        }
        if (expectOperand) return "error";  // 以运算符结尾

        // 3. 构建表达式树
        ExpTreeNode* root = build(tokens);
        if (!root) return "error";

        // 4. 后序遍历计算布尔值
        bool result = evaluateTree(root);

        // 5. 释放内存
        releaseTree(root);

        return result ? "true" : "false";
    }

private:
    // 返回运算符优先级,操作数优先级最高
    int getPriority(const string &op) {
        if (op == "or")   return 1;
        if (op == "and")  return 2;
        return INT_MAX;   // true / false
    }

    // 构建表达式树(基于优先级,左结合)
    ExpTreeNode* build(const vector<string> &tokens) {
        stack<ExpTreeNode*> sta;
        for (const string &t : tokens) {
            int priority = getPriority(t);
            ExpTreeNode* node = new ExpTreeNode(t);
            node->priority = priority;

            while (!sta.empty() && sta.top()->priority >= priority) {
                ExpTreeNode* top = sta.top();
                sta.pop();
                int left_priority = INT_MIN;
                if (!sta.empty()) {
                    left_priority = sta.top()->priority;
                }
                if (left_priority < priority) {
                    node->left = top;
                } else {
                    sta.top()->right = top;
                }
            }
            sta.push(node);
        }

        // 栈中剩余节点串成右斜树
        ExpTreeNode* root = nullptr;
        if (!sta.empty()) {
            root = sta.top();
            sta.pop();
            ExpTreeNode* right = root;
            while (!sta.empty()) {
                root = sta.top();
                sta.pop();
                root->right = right;
                right = root;
            }
        }
        return root;
    }

    // 后序遍历求值
    bool evaluateTree(ExpTreeNode* root) {
        if (!root) return false;
        if (root->symbol == "true")  return true;
        if (root->symbol == "false") return false;

        bool leftVal  = evaluateTree(root->left);
        bool rightVal = evaluateTree(root->right);

        if (root->symbol == "and") return leftVal && rightVal;
        if (root->symbol == "or")  return leftVal || rightVal;
        return false; // 不可达
    }

    // 递归释放树节点
    void releaseTree(ExpTreeNode* root) {
        if (!root) return;
        releaseTree(root->left);
        releaseTree(root->right);
        delete root;
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值