《Cracking the Coding Interview》——第9章:递归和动态规划——题目6
2014-03-20 03:27
题目:输出所有由N对括号组成的合法的括号序列。比如n=2,“()()”、“(())”等等。
解法:动态规划配合DFS,应该也叫记忆化搜索吧。一个整数N总可以拆成若干个正整数的和,执行搜索的时候也是按照这个规则,将N序列拆成多个子序列进行搜索,同时将中间的搜索结果记录下来,以便下次再搜到的时候直接调用,省掉重复计算的开销。
代码:
// 9.6 Print all valid parentheses sequences of n ()s.
#include <iostream>
#include <string>
#include <vector>
using namespace std; void DFS(int idx, int n, string s, vector<vector<string> > &result)
{
if (idx == n) {
result[n].push_back(s);
return;
} else {
int i, j;
for (i = ; i <= n - idx; ++i) {
for (j = ; j < (int)result[i - ].size(); ++j) {
DFS(idx + i, n, s + '(' + result[i - ][j] + ')', result);
}
}
}
} int main()
{
vector<vector<string> > result;
int n;
int i; result.resize();
result[].push_back(""); while (cin >> n && n > ) {
if (n >= (int)result.size()) {
for (i = (int)result.size(); i <= n; ++i) {
result.push_back(vector<string>());
DFS(, i, "", result);
}
}
for (i = ; i < (int)result[n].size(); ++i) {
cout << result[n][i] << endl;
}
} return ;
}
《Cracking the Coding Interview》——第9章:递归和动态规划——题目6的更多相关文章
- 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》——第9章:递归和动态规划——题目11
2014-03-21 20:20 题目:给定一个只包含‘0’.‘1’.‘|’.‘&’.‘^’的布尔表达式,和一个期望的结果(0或者1).如果允许你用自由地给这个表达式加括号来控制运算的顺序,问 ...
- 《Cracking the Coding Interview》——第9章:递归和动态规划——题目10
2014-03-20 04:15 题目:你有n个盒子,用这n个盒子堆成一个塔,要求下面的盒子必须在长宽高上都严格大于上面的.如果你不能旋转盒子变换长宽高,这座塔最高能堆多高? 解法:首先将n个盒子按照 ...
- 《Cracking the Coding Interview》——第9章:递归和动态规划——题目9
2014-03-20 04:08 题目:八皇后问题. 解法:DFS解决. 代码: // 9.9 Eight-Queen Problem, need I say more? #include <c ...
随机推荐
- 西门子(SIEMENS)软件安装时需要重启的解决方法,regedit restart
打开注册表(regedit) 删除注册表项 HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manage\PendingFile ...
- 华硕主板开启intel virtual technology以便支持虚拟机
- May 6th 2017 Week 18th Saturday
A great ship asks deep water. 巨轮寻深水而航行. A great ship needs deep water so as to get enough buoyancy t ...
- 四大CPU体系结构ARM、X86/Atom、MIPS、PowerPC
http://blog.csdn.net/wangjianno2/article/details/52140936 RISC(reduced instruction set computer,精简指令 ...
- 模拟插队,出队,POJ(2259)
题目链接:http://poj.org/problem?id=2259 水题一个,就是要记录一下每个队列是否有人bool[i], #include <iostream> #include ...
- 网格中的BFS,逆向(POJ2049)
题目链接:http://poj.org/problem?id=2049 解题报告: 网格中的BFS,最主要的是边界问题. 1.这里在左右,上下两个方向上,分别判断墙,和门,细节是,向上有t个墙,for ...
- http知识补充
在我的职业生涯中,没怎么重视过这http四个字,想当然的觉得不就是个网页请求嘛就没怎么当回事,而且很多http相关的长篇大论一听就困,真心是弄不下去,但是就是这种观念导致我后期的工作中不断的挖坑,不断 ...
- apache配置局域网访问
1.配置vhost.conf NameVirtualHost 192.168.2.74:80 <VirtualHost 192.168.2.74:80> DocumentRoot /var ...
- xpath模块
W3School官方文档:http://www.w3school.com.cn/xpath/index.asp 我们使用xpath主要是获取网页数据的,之前一直是使用bs4,xpath也是最近了解到的 ...
- CSS中margin: 0 auto;样式没有生效
问题:有两个元素: A, B.两则是嵌套关系,A是B的父节点.A和B都是块元素.当在A上设置:margin: 0 auto的时候,B并没有在页面中居中. margin: 0 auto 为什么没有生效? ...