• 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));
    
        }
    }
  • 相关阅读:
    国外优秀的icon设计站点
    HDU 2289 Cup (二分)
    HDU 1709 The Balance (母函数 * *)
    HDU 2152 Fruit (母函数)
    POJ 3294 Life Forms (后缀数组)
    HDU 2152 选课时间(题目已修改,注意读题) (母函数)
    HDU 3278 Puzzle (蛋疼。。。。)
    HDU The Rotation Game
    HDU 2069 Coin Change (母函数 | 背包 )
    HDU 2899 Strange fuction (二分)
  • 原文地址:https://www.cnblogs.com/jdflyfly/p/3932615.html
Copyright © 2020-2023  润新知