Q9.11 count the number of ways of parenthesizing the expression

Q:Given a boolean expression consisting of the symbols 0,1, &, /, and A, and a desired boolean result value result, implement a function to count the number of ways of parenthesizing the expression such that it evaluates to result.

A:递归问题,找到当前问题与子问题的关系。

通过对表达式加括号,将其划分为子问题,

例如 f(1&0&1|1, true) = f( 1(&0&1|1), true) +  f( (1&0)&(1|1), true) +  f( (1&0&1)|1, true)。 通过这个例子可以总结出划分规律, 将问题分成两部分,i和n-i, 其中i指1和0的个数,n指整个表达式中1和0的个数,i = 1, 2..n-1.

进而再利用真值表就可以求得总的个数。

#include <iostream>
#include <string>
#include <map>
using namespace std;

typedef map<int, int> mi;

int placeParen(string exp, int result, int start, int end, mi &Map){
	int key = result*200 + start*7 + end*3;
	if (Map.count(key))
		return Map[key];
	if (start == end) {
		if (exp[start] == '1' && result == 1)
			return 1;
		else if (exp[start] == '0' && result == 0)
			return 1;
		return 0;
	}
	int c=0;
	if (result) {
		for (int i = start + 1; i < end; i+=2) {
			char op = exp[i];
			if (op == '&')
				c += placeParen(exp, 1, start, i-1, Map) * placeParen(exp, 1, i+1, end, Map);
			else if (op == '|') {
				c += placeParen(exp, 1, start, i-1, Map) * placeParen(exp, 0, i+1, end, Map);
				c += placeParen(exp, 0, start, i-1, Map) * placeParen(exp, 1, i+1, end, Map);
				c += placeParen(exp, 1,start, i-1, Map) * placeParen(exp, 1, i+1, end, Map);
			}
			else if (op == '^' ) {
				c += placeParen(exp, 1, start, i-1, Map) * placeParen(exp, 0, i+1, end, Map);
				c += placeParen(exp, 0, start, i-1, Map) * placeParen(exp, 1, i+1, end, Map);
			}
		}
	}
	else{
		for (int i = start + 1; i < end; i+=2) {
			char op = exp[i];
			if (op == '&') {
				c += placeParen(exp, 0, start, i-1, Map) * placeParen(exp, 1, i+1, end, Map);
				c += placeParen(exp, 1, start, i-1, Map) * placeParen(exp, 0, i+1, end, Map);
				c += placeParen(exp, 0, start, i-1, Map) * placeParen(exp, 0, i+1, end, Map);
			}
			else if (op == '|')
				c += placeParen(exp, 0, start, i-1, Map) * placeParen(exp, 0, i+1, end, Map);
			else if (op == '^' ) {
				c += placeParen(exp, 1, start, i-1, Map) * placeParen(exp, 1, i+1, end, Map);
				c += placeParen(exp, 0, start, i-1, Map) * placeParen(exp, 0, i+1, end, Map);
			}
		}
	}
	Map[key] = c;
	return c;
}

int main(){
	mi Map;
	string s = "1&0&1|1";
	cout<<placeParen(s, 1, 0, s.length()-1, Map);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值