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. 概念:详细讲解url和路由概念

    例如:一个网址为 http://www.abc.com/aa 定义:/aa = bb/cc/dd 那么:http://www.abc.com/aa就是一个url,那么我们可以得出:网址=url 而当我 ...

  2. HTML?这些还不懂咋办?

    1.什么是空白折叠现象?为什么要空白折叠呢? 对于我们大多数人的习惯来讲,大都喜欢利用空格或者换行来调整文章的文字结构.这样往往可以使我们可以更轻松的阅读.但是,在HTML中却不允许我们这么做,这是为 ...

  3. Android_ListView适配器

    ListView如何优化 复用convertView缓存(减少ListView绘制). 自定义静态类ViewHolder(减少findViewById次数),通过setTag().getTag()获取 ...

  4. ADO.NET之一:连接层

    ADO.NET大部分由System.Data.dll核心程序集来表示. ADO.NET类库有三种完全不听的方式来实现数据访问:连接式.断开式和通过Entity框架.连接式就是会一直占用网络资源,断开式 ...

  5. System.TimeDate

    本篇将介绍时间类型. msdn官网:点击查看 时间辅助类:点击查看  方法:计算两个时间的时间差(年月日小时分钟),获取时间戳,时间格式转换,获取时间随机码 定义:表示时间上的一刻,通常以日期和当天的 ...

  6. pooling

    转自:http://www.gageet.com/2014/09182.php 本文部分参考了:http://www.zhihu.com/question/23437871 卷积层是对图像的一个邻域进 ...

  7. JS里访问纯数字ID对象时出现问题

    <p id="1">Hello</p> 例如上面的例子,id为纯数字会出错.此时用js去获取该对象会报错,查找不到该对象. 原因:必须是字母开头,或是下划线 ...

  8. CRC32为例详细解析(菜鸟至老鸟进阶)

    CRC-知识解析 cyclic redundancy check 写在前面的话: 之前在做学校项目的时候用到了CRC 原理,但在网上查找的过程中,发现讲解CRC知识的资源很多,但是对新手比较友好的.讲 ...

  9. 基于centos7实现的ftp

    前言 FTP(File transfer Protocl),文件传输协议,用于在网络上进行文件传输的一套标准协议,使用客户/服务器模式,属于网络传输协议的应用层.FTP服务运行在TCP/21和20端口 ...

  10. 【PHP】Maximum execution time of 30 seconds exceeded解决办法

    Maximum execution time of 30 seconds exceeded,今天把这个错误的解决方案总结一下: 简单总结一下解决办法: 报错一:内存超限,具体报错语句忘了,简单说一下解 ...