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() == ) {
if (exp[] - '' == target) {
path.push_back(one_path);
++result;
}
return;
} string new_exp;
int i, j;
for (i = ; i < (int)exp.length(); i += ) {
new_exp = ""; for (j = ; j < i - ; ++j) {
new_exp.push_back(exp[j]);
} if (exp[i] == '&') {
new_exp.push_back(((exp[i - ] - '') & (exp[i + ] - '')) + '');
} else if (exp[i] == '|') {
new_exp.push_back(((exp[i - ] - '') | (exp[i + ] - '')) + '');
} else if (exp[i] == '^') {
new_exp.push_back(((exp[i - ] - '') ^ (exp[i + ] - '')) + '');
} for (j = i + ; 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 = ;
one_path.push_back(exp);
booleanExpressionParentheses(exp, target, result, one_path, path);
one_path.pop_back();
for (i = ; i < (int)path.size(); ++i) {
cout << "Path " << i + << endl;
for (j = ; j < (int)path[i].size(); ++j) {
cout << path[i][j] << endl;
}
cout << endl;
} for (i = ; 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 ;
}

《Cracking the Coding Interview》——第9章:递归和动态规划——题目11的更多相关文章

  1. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  2. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  3. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  4. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  5. Cracking the Coding Interview(Stacks and Queues)

    Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...

  6. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  7. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

  8. cracking the coding interview系列C#实现

    原版内容转自:CTCI面试系列——谷歌面试官经典作品 | 快课网 此系列为C#实现版本 谷歌面试官经典作品(CTCI)目录   1.1 判断一个字符串中的字符是否唯一 1.2 字符串翻转 1.3 去除 ...

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

    2014-03-20 04:15 题目:你有n个盒子,用这n个盒子堆成一个塔,要求下面的盒子必须在长宽高上都严格大于上面的.如果你不能旋转盒子变换长宽高,这座塔最高能堆多高? 解法:首先将n个盒子按照 ...

随机推荐

  1. IOS通讯录的隐藏标签【电话】的特殊功能(在IOS11已失效)

    这功能比较适合有强迫症,爱折腾的人哈!! 规范了通讯录标签,以后可以轻松的知道别人是用短号还是亲情网给你打电话. 如果是长号还可以显示归属地. 也许从IOS8(不太清楚)开始自带了号码归属地显示功能, ...

  2. Kalman filter, Laser/Lidar measurement

    You can download this project from https://github.com/lionzheng10/LaserMeasurement The laser measure ...

  3. redis网络模型

    多路IO复用-非阻塞同步IO模型.见http://www.cnblogs.com/syyong/p/6231326.html 具体结构:http://blog.jobbole.com/100079/ ...

  4. 计算最大矩形面积,POJ(2082)

    题目链接:http://poj.org/problem?id=2082 把矩形按照高度一次递增的循序排列,当违反这一规则的时候,更新ans,用新的data替换之前的矩形.然后最后扫一遍. #inclu ...

  5. linux命令之awk命令

    awk是一种编程语言,用于在linux/unix下对文本和数据进行处理.数据可以来自标准输入(stdin).一个或多个文件,或其它命令的输出.它支持用户自定义函数和动态正则表达式等先进功能,是linu ...

  6. ORA-01262,oracle启动报错,及Oracle启动原理

    错误状态: SQL> startup ORA-01261: Parameter db_recovery_file_dest destination string cannot be transl ...

  7. laravel 去掉index.php伪静态

    1,首先,让apache服务器支持rewrite 可以在apache配置文件中定义rewrite规则,是全局的,无论哪个应用都实用 //httpd.config Listen 80 RewriteEn ...

  8. PureLayout,使用纯代码写AutoLayout

    为iOS和OS X的自动布局最终的API -- 令人印象深刻的简单,非常强大. PureLayout延伸的UIView /NSView , NSArray,和NSLayoutConstraint与之后 ...

  9. ionic 命令cordova

    安装android platform : ionic platform add android 安装一维码cordova插件 :cordova plugin add https://github.co ...

  10. 协程实现tcp两个客户端的通讯

    import socket import gevent from gevent import monkey monkey.patch_all() def cb_work(recv_num,send_n ...