9.11 given a Boolean expression, it consists of 0, 1, &, |, ^, and other symbols, as well as a desired boolean result to implement a function, this expression can be used to obtain the result value by calculating the placement of several parentheses.

Source: Internet
Author: User

 

Idea: recursive solution of enumeration Split points, which can be optimized by DP.

Pay attention to recursive termination conditions.

Note ^ & | there are three different statistical conditions.

 

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));    }}

 

9.11 given a Boolean expression, it consists of 0, 1, &, |, ^, and other symbols, as well as a desired boolean result to implement a function, this expression can be used to obtain the result value by calculating the placement of several parentheses.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.