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. April 14 2017 Week 15 Friday

    Try to be a rainbow in someone's cloud. 当乌云萦绕心头,我愿意成为你的彩虹. Actually there are many rainbows in our l ...

  2. Raknet—视频会议系统最佳的数据传输引擎

    RakNet是一个跨平台的C++和C#的游戏引擎,它主要是为高效的数据传输而设计,使用者可以通过它进行游戏和其他的程序的开发.RakNet虽然是一个游戏引擎,但同样也是一个非常好的视频会议系统传输引擎 ...

  3. 【转】jpg png区别和使用

    为什么想整理这方面的类容,我觉得就像油画家要了解他的颜料和画布.雕塑家要了解他的石材一样,作为网页设计师也应该对图片格式的特性有一定了解,这样才能更好的表达你的创意和想法. 除此之外,我们在平时工作中 ...

  4. 利用API设置桌面背景

    实现效果: 知识运用: API函数SystemParametersInfo 实现代码: [DllImport("user32.dll", EntryPoint = "Sy ...

  5. Spring Security 实现手机验证码登录

    思路:参考用户名密码登录过滤器链,重写认证和授权 示例如下(该篇示例以精简为主,演示主要实现功能,全面完整版会在以后的博文中发出): 由于涉及内容较多,建议先复制到本地工程中,然后在细细研究. 1. ...

  6. PHP中的生成XML文件的4种方法分享

    生成如下XML串 Xml代码 <?xml version="1.0" encoding="utf-8"?> <article> < ...

  7. FAT32中文版分析+补充(1)

    概述 起先所有的FAT文件系统都是为IBM PC机器而设计的,这说明了一个重要的问题:FAT文件系统在磁盘上的数据是用“小端”(Little Endian)结构存储的.我们使用4个8-bit的字节—— ...

  8. Java分享笔记:使用缓冲流复制文件

    [1] 程序设计 /*------------------------------- 1.缓冲流是一种处理流,用来加快节点流对文件操作的速度 2.BufferedInputStream:输入缓冲流 3 ...

  9. 【杂题总汇】Codeforces-67A Partial Teacher

    [Codeforces-67A]Partial Teacher 上周刷了一大堆小紫薯的动态规划的题

  10. 私有maven库发布及使用流程

    ## 私有maven库发布流程 ### 环境配置 - idea环境下,如果使用内置maven,需要手动生成settings.xml,并关联. - 操作如下 - 生成settings.xml 右键pom ...