您的位置:首页 > 其它

《Cracking the Coding Interview》——第9章:递归和动态规划——题目11

2014-03-21 20:32 447 查看
2014-03-21 20:20

题目:给定一个只包含‘0’、‘1’、‘|’、‘&’、‘^’的布尔表达式,和一个期望的结果(0或者1)。如果允许你用自由地给这个表达式加括号来控制运算的顺序,问问有多少种加括号的方法来达到期望的结果值。

解法:DFS暴力解决,至于优化方法,应该是可以进行一部分剪枝的,但我没想出能明显降低复杂度的方法。

代码:

// 9.11 For a boolean expression and a target value, calculate how many ways to use parentheses on the expression to achieve that value.
#include <iostream>
#include <string>
#include <vector>
using namespace std;

void booleanExpressionParentheses(string exp, int target, int &result, vector<string> &one_path, vector<vector<string> > &path)
{
if ((int)exp.length() == 1) {
if (exp[0] - '0' == target) {
path.push_back(one_path);
++result;
}
return;
}

string new_exp;
int i, j;
for (i = 1; i < (int)exp.length(); i += 2) {
new_exp = "";

for (j = 0; j < i - 1; ++j) {
new_exp.push_back(exp[j]);
}

if (exp[i] == '&') {
new_exp.push_back(((exp[i - 1] - '0') & (exp[i + 1] - '0')) + '0');
} else if (exp[i] == '|') {
new_exp.push_back(((exp[i - 1] - '0') | (exp[i + 1] - '0')) + '0');
} else if (exp[i] == '^') {
new_exp.push_back(((exp[i - 1] - '0') ^ (exp[i + 1] - '0')) + '0');
}

for (j = i + 2; j < (int)exp.length(); ++j) {
new_exp.push_back(exp[j]);
}
one_path.push_back(new_exp);
booleanExpressionParentheses(new_exp, target, result, one_path, path);
one_path.pop_back();
}
}

int main()
{
int result;
int target;
int i, j;
string exp;
vector<vector<string> > path;
vector<string> one_path;

while (cin >> exp >> target) {
result = 0;
one_path.push_back(exp);
booleanExpressionParentheses(exp, target, result, one_path, path);
one_path.pop_back();
for (i = 0; i < (int)path.size(); ++i) {
cout << "Path " << i + 1 << endl;
for (j = 0; j < (int)path[i].size(); ++j) {
cout << path[i][j] << endl;
}
cout << endl;
}

for (i = 0; i < (int)path.size(); ++i) {
path[i].clear();
}
path.clear();
one_path.clear();

cout << "Total number of ways to achieve the target value is " << result << "." << endl;
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐