编辑代码

import java.util.*;

public class Main {
    // 手动解析和计算布尔表达式
    public static boolean evaluateExpression(String expression, Map<String, String> keyValueMap) {
        // 替换表达式中的变量为实际的值
        for (Map.Entry<String, String> entry : keyValueMap.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            expression = expression.replaceAll(key + " = '" + value + "'", "true");
            expression = expression.replaceAll(key + " = '[^']*'", "false");
        }

        // 移除不需要的引号,处理逻辑运算符
        expression = expression.replaceAll("AND", "&&");
        expression = expression.replaceAll("OR", "||");

        // 计算表达式
        return calculate(expression);
    }

    // 简单的表达式求值(仅处理 && 和 ||)
    public static boolean calculate(String expression) {
        Stack<Boolean> stack = new Stack<>();
        Stack<String> operators = new Stack<>();
        expression = expression.replaceAll("\\s+", "");  // 移除空格
        int i = 0;

        while (i < expression.length()) {
            if (expression.charAt(i) == '(') {
                operators.push("(");
                i++;
            } else if (expression.charAt(i) == ')') {
                while (!operators.isEmpty() && !"(".equals(operators.peek())) {
                    boolean b2 = stack.pop();
                    boolean b1 = stack.pop();
                    String op = operators.pop();
                    stack.push(applyOperator(b1, b2, op));
                }
                operators.pop();  // 弹出'('
                i++;
            } else if (expression.startsWith("true", i)) {
                stack.push(true);
                i += 4;
            } else if (expression.startsWith("false", i)) {
                stack.push(false);
                i += 5;
            } else if (expression.startsWith("&&", i)) {
                operators.push("&&");
                i += 2;
            } else if (expression.startsWith("||", i)) {
                operators.push("||");
                i += 2;
            }
        }

        // 计算剩余的操作符
        while (!operators.isEmpty()) {
            boolean b2 = stack.pop();
            boolean b1 = stack.pop();
            String op = operators.pop();
            stack.push(applyOperator(b1, b2, op));
        }

        return stack.pop();
    }

    // 应用运算符 && 或 || 进行逻辑运算
    public static boolean applyOperator(boolean b1, boolean b2, String op) {
        if (op.equals("&&")) {
            return b1 && b2;
        } else if (op.equals("||")) {
            return b1 || b2;
        }
        return false;  // 不应该到达这里
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        // 读取n和m
        int n = sc.nextInt();
        int m = sc.nextInt();
        sc.nextLine();  // 跳过换行符

        // 读取表达式
        String[] expressions = new String[n];
        for (int i = 0; i < n; i++) {
            expressions[i] = sc.nextLine();
        }

        // 读取键值对
        Map<String, String> keyValueMap = new HashMap<>();
        for (int i = 0; i < m; i++) {
            String key = sc.next();
            String value = sc.next();
            keyValueMap.put(key, value);
        }

        // 遍历每个表达式并进行计算
        for (String expr : expressions) {
            boolean result = evaluateExpression(expr, keyValueMap);
            // 输出结果:0表示健康,1表示不健康
            if (result) {
                System.out.println(0);  // 健康
            } else {
                System.out.println(1);  // 不健康
            }
        }

        sc.close();
    }
}