《Cracking the Coding Interview》——第17章:普通题——题目14
2014-04-29 00:20
题目:给定一个长字符串,和一个词典。如果允许你将长串分割成若干个片段,可能会存在某些片段在词典里查不到,有些则查得到。请设计算法进行分词,使得查不到的片段个数最少。
解法:用空间换取时间的动态规划算法,首先用O(n^2)的时间判断每一个片段是否在字典里。这个过程其实可以通过字典树来进行加速,时间上能优化一个阶,不过我没写,偷懒用<unordered_set>代表了字典。之后通过O(n)时间的动态规划,dp[i]表示当前位置的查不到的片段的最少个数。对于懂代码的人,代码说的比文字清楚,所以请看代码。
代码:
// 17.14 Given a dictionary of words, and a long string. You may find a way to cut the string into words, where some of them may or may not be in the dictionary.
// Dynamic programming is a good thing, but trades space in for time.
#include <iostream>
#include <string>
#include <unordered_set>
#include <vector>
using namespace std; int main()
{
string data;
unordered_set<string> dict;
vector<vector<bool> > contains;
vector<int> dp;
int i, j;
string s;
int n;
int tmp; while (cin >> data && data != "") {
cin >> n;
for (i = ; i < n; ++i) {
cin >> s;
dict.insert(s);
}
n = (int)data.length(); contains.resize(n);
for (i = ; i < n; ++i) {
contains[i].resize(n);
}
for (i = ; i < n; ++i) {
s = "";
for (j = i; j < n; ++j) {
s.push_back(data[j]);
contains[i][j] = (dict.find(s) != dict.end());
}
} dp.resize(n);
for (i = ; i < n; ++i) {
dp[i] = contains[][i] ? : i + ;
for (j = ; j < i; ++j) {
tmp = dp[j] + (contains[j + ][i] ? : i - j);
dp[i] = dp[i] < tmp ? dp[i] : tmp;
}
} printf("%d\n", dp[n - ]); for (i = ; i < n; ++i) {
contains[i].clear();
}
contains.clear();
dp.clear();
dict.clear();
} return ;
}
《Cracking the Coding Interview》——第17章:普通题——题目14的更多相关文章
- 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目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- 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》——第18章:难题——题目13
2014-04-29 04:40 题目:给定一个字母组成的矩阵,和一个包含一堆单词的词典.请从矩阵中找出一个最大的子矩阵,使得从左到右每一行,从上到下每一列组成的单词都包含在词典中. 解法:O(n^3 ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- 《Cracking the Coding Interview》——第17章:普通题——题目13
2014-04-29 00:15 题目:将二叉搜索树展开成一个双向链表,要求这个链表仍是有序的,而且不能另外分配对象,就地完成. 解法:Leetcode上也有,递归解法. 代码: // 17.13 F ...
随机推荐
- 如何在windows下安装配置pyspark notebook
第一步:安装anaconda anaconda自带一系列科学计算包 下载链接:http://pan.baidu.com/s/1b4jWlg 密码:fqq3 接着配置环境变量:如我安装在D盘下 试一 ...
- ubuntu 显示隐藏文件
原文链接 http://blog.csdn.net/happyjiahan/article/details/6023496 方法1.使用命令ls -a显示所有的文件,包括隐藏文件 方法2.在桌面化操作 ...
- 第三章 八位数字开关板&模拟输入板&火焰传感器
这节我将带大家了解亮宁机器人基础外接硬件. 八位数字板开关 接线方法:W1~W8接23~37号数字端口,Enter接39号数字端口,vcc和gnd分别接正负. #include <LNDZ.h& ...
- c++ priority_queue
1.默认为大顶堆 #include <iostream> #include <queue> using namespace std; int main() { priority ...
- framework7 手风琴页面有滚动条时再次点开手风琴item滑动里面内容消失的解决方法
在手风琴的ul外面的div加入最小高度min-height:1000px,问题解决 示例代码: <div class="list-block accordion-list" ...
- hdu-3015 Disharmony Trees---离散化+两个树状数组
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3015 题目大意: 有一些树,这些树的高度和位置给出.现在高度和位置都按从小到大排序,对应一个新的ra ...
- 【UOJ83】【UR #7】水题出题人(提交答案题)
点此看题面 大致题意: 给你若干份排序的代码,共\(6\)个子任务,每个子任务让你构造数据使得一份代码用时在给定的\(T\)以内,另一份代码用时超过\(2000000\). 子任务\(1\):归并排序 ...
- 百度Ueditor 图片上传无反应,显示上传0张,不能点确定
解决办法: \Data\Ueditor\php\Uploader.class.php 190行左右 /** * 获取文件扩展名 * @return string */ private function ...
- 整个trick
数据输入方面:1.image pyramid 图像金字塔.目前代码里是先选取一个scale,然后在每个GPU上按照scale读图片,相应的gt也更改."scales":[440, ...
- ztree3.5中checkbox无法取消选中的问题解决
使用jquery的tree插件ztree,用到复选框的功能 var setting = { check: { enable: true }, data: { simpleData: { enable: ...