《Cracking the Coding Interview》——第9章:递归和动态规划——题目11
2014-03-21 20:32
447 查看
2014-03-21 20:20
题目:给定一个只包含‘0’、‘1’、‘|’、‘&’、‘^’的布尔表达式,和一个期望的结果(0或者1)。如果允许你用自由地给这个表达式加括号来控制运算的顺序,问问有多少种加括号的方法来达到期望的结果值。
解法:DFS暴力解决,至于优化方法,应该是可以进行一部分剪枝的,但我没想出能明显降低复杂度的方法。
代码:
题目:给定一个只包含‘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; }
相关文章推荐
- 《Cracking the Coding Interview》——第9章:递归和动态规划——题目6
- 《Cracking the Coding Interview》——第9章:递归和动态规划——题目7
- 《Cracking the Coding Interview》——第9章:递归和动态规划——题目8
- 《Cracking the Coding Interview》——第9章:递归和动态规划——题目9
- 《Cracking the Coding Interview》——第9章:递归和动态规划——题目10
- 《Cracking the Coding Interview》——第9章:递归和动态规划——题目1
- 《Cracking the Coding Interview》——第9章:递归和动态规划——题目2
- 《Cracking the Coding Interview》——第9章:递归和动态规划——题目3
- 《Cracking the Coding Interview》——第9章:递归和动态规划——题目4
- 《Cracking the Coding Interview》——第9章:递归和动态规划——题目5
- 《Cracking the Coding Interview》——第18章:难题——题目11
- 《Cracking the Coding Interview》——第17章:普通题——题目11
- 《Cracking the Coding Interview》——第10章:可扩展性和存储空间限制——题目1
- 《Cracking the Coding Interview》——第13章:C和C++——题目6
- 《Cracking the Coding Interview》——第16章:线程与锁——题目6
- 《Cracking the Coding Interview》——第17章:普通题——题目14
- 《Cracking the Coding Interview》——第1章:数组和字符串——题目7
- 《Cracking the Coding Interview》——第4章:树和图——题目4
- 《Cracking the Coding Interview》——第6章:智力题——题目2
- 《Cracking the Coding Interview》——第6章:智力题——题目4