zoukankan      html  css  js  c++  java
  • 9.11 给定一个布尔表达式,由0、1、&、|、^等符号组成,以及一个想要的布尔结果result,实现一个函数,算出有几种括号的放法可使该表达式得出result的值。

    思路: 枚举分割点递归求解,可用DP优化。

      注意递归终止条件。

      注意 ^ & | 三种情况统计的不同。

    import java.util.HashMap;
    import java.util.Map;
    
    public class Solution {
    
        int countR(String terms, boolean result, int start, int end, Map<String, Integer> cache) {
            String key = "" + result + start + end;
            if (cache.containsKey(key)) {
                return cache.get(key);
            }
    
            if (start == end) {
                if (terms.charAt(start) == '0') {
                    if (result)
                        return 0;
                    else
                        return 1;
                } else {
                    if (result)
                        return 1;
                    else
                        return 0;
                }
    
            }
            int count = 0;
            if (result) {
                for (int i = start + 1; i < end; i++) {
                    char op = terms.charAt(i);
                    if (op == '&') {
                        count += countR(terms, true, start, i - 1, cache) * countR(terms, true, i + 1, end, cache);
                    } else if (op == '|') {
                        count += countR(terms, true, start, i - 1, cache) * countR(terms, true, i + 1, end, cache);
                        count += countR(terms, true, start, i - 1, cache) * countR(terms, false, i + 1, end, cache);
                        count += countR(terms, false, start, i - 1, cache) * countR(terms, true, i + 1, end, cache);
    
                    } else if (op == '^') {
                        count += countR(terms, true, start, i - 1, cache) * countR(terms, false, i + 1, end, cache);
                        count += countR(terms, false, start, i - 1, cache) * countR(terms, true, i + 1, end, cache);
                    }
    
                }
    
            } else {
                for (int i = start + 1; i < end; i++) {
                    char op = terms.charAt(i);
                    if (op == '&') {
                        count += countR(terms, true, start, i - 1, cache) * countR(terms, false, i + 1, end, cache);
                        count += countR(terms, false, start, i - 1, cache) * countR(terms, true, i + 1, end, cache);
                        count += countR(terms, false, start, i - 1, cache) * countR(terms, false, i + 1, end, cache);
    
                    } else if (op == '|') {
                        count += countR(terms, false, start, i - 1, cache) * countR(terms, false, i + 1, end, cache);
    
                    } else if (op == '^') {
                        count += countR(terms, true, start, i - 1, cache) * countR(terms, true, i + 1, end, cache);
                        count += countR(terms, false, start, i - 1, cache) * countR(terms, false, i + 1, end, cache);
                    }
    
                }
    
            }
            cache.put(key, count);
            return count;
        }
    
        public static void main(String[] args) {
            String terms = "0^0|1&1^1|0|1";
            boolean result = true;
            Map<String, Integer> cache = new HashMap<String, Integer>();
            System.out.println(new Solution().countR(terms, result, 0, terms.length() - 1, cache));
    
        }
    }
  • 相关阅读:
    ThreadLocal源码分析与实践
    基于jdk1.8的LinkedList源码分析
    Spring编程式事务使用不当导致其他事务无法正常提交
    Spring计时器StopWatch使用
    工厂模式(Factory pattern)
    Spring Cloud Alibaba生态探索:Dubbo、Nacos及Sentinel的完美结合
    Spring Cloud Alibaba微服务生态的基础实践
    自己作图分析分布式技术架构演化的常用套路
    用一个实例项目重新认识分布式系统
    重温Java Web的技术细节
  • 原文地址:https://www.cnblogs.com/jdflyfly/p/3932615.html
Copyright © 2011-2022 走看看