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));
    
        }
    }
  • 相关阅读:
    关于 No buffer space available (maximum connections reached?): connect 的处理
    Cron 表达式
    Hudson 打包部署到Was上特别慢
    JAVA jar 参数
    CentOS 6 UNEXPECTED INCONSISTENCY RUN fsck MANUALLY
    SSH 连接很慢
    解决libc.so.6: version `GLIBC_2.14&#39; not found问题
    Maven 基本参数
    Shc 应用
    JAVA 关于JNI本地库加载
  • 原文地址:https://www.cnblogs.com/jdflyfly/p/3932615.html
Copyright © 2011-2022 走看看