《Cracking the Coding Interview》——第9章:递归和动态规划——题目11
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的更多相关文章
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- 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 ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- cracking the coding interview系列C#实现
原版内容转自:CTCI面试系列——谷歌面试官经典作品 | 快课网 此系列为C#实现版本 谷歌面试官经典作品(CTCI)目录 1.1 判断一个字符串中的字符是否唯一 1.2 字符串翻转 1.3 去除 ...
- 《Cracking the Coding Interview》——第9章:递归和动态规划——题目10
2014-03-20 04:15 题目:你有n个盒子,用这n个盒子堆成一个塔,要求下面的盒子必须在长宽高上都严格大于上面的.如果你不能旋转盒子变换长宽高,这座塔最高能堆多高? 解法:首先将n个盒子按照 ...
随机推荐
- Multi-modal Sentence Summarization with Modality Attention and Image Filtering 论文笔记
文章已同步更新在https://ldzhangyx.github.io/,欢迎访问评论. 五个月没写博客了,不熟悉我的人大概以为我挂了…… 总之呢这段时间还是成长了很多,在加拿大实习的两个多月来 ...
- v-for的深层用法
为了提升循环的性能,我们会给循环加上一个唯一的key值,这个key值一定是唯一的 <div id='root'> <div v-for='(item,index) of list' ...
- Oracle数据库几种启动方式及查询当前状态
Oracle数据库几种启动方式 1.startup nomount: 非安装启动,这种方式下启动可执行:重建控制文件.重建数据库,读取init.ora文件,启动instance,即启动SGA和后台进程 ...
- python 3+djanjo 2.0.7简单学习(二)--创建数据库和模型
我们紧接上次,这里将建立数据库,创建第一个模型提示:这里我们不需要去一直启动,django会在我们ctrl+s的时候自动刷新并启动服务,很方便吧 1.数据库配置 现在,打开 vote_mysite/ ...
- Spring Security 实现手机验证码登录
思路:参考用户名密码登录过滤器链,重写认证和授权 示例如下(该篇示例以精简为主,演示主要实现功能,全面完整版会在以后的博文中发出): 由于涉及内容较多,建议先复制到本地工程中,然后在细细研究. 1. ...
- android build.prop详解
# begin build properties开始设置系统性能 # autogenerated by buildinfo.sh{通过设置形成系统信息} ro.build.id=MIUI(版本ID) ...
- Mac下安装OpenCV3.0和Anaconda和环境变量设置
入手Mac几天了,想在Mac OS下玩玩OpenCV和keras,间歇捣鼓了两天,终于搞定zsh.OpenCV3.0以及Anaconda.OpenCV3.0刚发布不久,这方面的资料也不是很多,能够查到 ...
- python—命名空间、作用域查找顺序、闭包
名称空间 name space,如下图: x = 1, 1存放在内存中,1 会有一个内存地址,x 则 存放在 name space 里,并同时记录了 1的内存地址, 即 名称空间是存放了变量x与1绑定 ...
- SpringMVC3中返回json字符串时500 Internal Server Error的处理方案
搭建 Spring3+MyBatis+Rest+BootStrap+JBPM项目环境后,测试发现了一个操蛋的问题.使用Spring MVC的自动类型转换为JSON时,后台数据List/Map获取完全正 ...
- Ext4.2 select 和 query 区别与联系
Ext.query和Ext.select的作用是一致的,同是根据CSS选择符查找出一个或多个元素.区别在于返回类型上.分别是:query方法返回的是JavaScript标准的数组类型:select方法 ...