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));
    
        }
    }
  • 相关阅读:
    Windbg使用
    C#与C++之间类型的对应
    Hook CreateProcess
    基于EasyHook实现监控explorer资源管理器文件复制、删除、剪切等操作
    GUID和UUID、CLSID、IID 区别及联系
    hook C++
    EasyHook Creating a remote file monitor
    hook工具
    EasyHook
    Hook exe 和 file
  • 原文地址:https://www.cnblogs.com/jdflyfly/p/3932615.html
Copyright © 2011-2022 走看看