《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 ...
随机推荐
- CRUD全栈式编程架构总结
这里放出实例代码 github.com/SkyvenXiong/HCC
- Last_IO_Errno: 1032
(一):更新找不到记录 1032 Last_SQL_Errno: 1032 Last_SQL_Error: Could not execute Update_rows ...
- redis redis的连接
昨天2017年12月26日,我刚刚从网上下载了redis.经过一天的摸索,踩了不少坑.昨天下午,比较磕磕巴巴,今天早上 终于比较完善地完成了一次小操作. 使用cmd的重要步骤 1.输入redis-se ...
- BCB::TClientSocket,TServerSocket控件
一,首先服务端开启监听 ServerSocket1->Port=StrToInt(5000); ServerSocket1->Active=true; ServerSocket1控件,响应 ...
- CUDA memory
原文链接 CUDA存储器类型: 每个线程拥有自己的register and loacal memory; 每个线程块拥有一块shared memory; 所有线程都可以访问global memory; ...
- caffe怎么把全连接层转成convolutional层
caffe中有把fc层转化为conv层的,其实怎么看参数都是不变的,对alex模型来说,第一个fc层的参数是4096X9216,而conv的维度是4096x256x6x6,因此参数个数是不变的,只是需 ...
- jsonp 请求和回传实现
JSONP最主要的是可以解决跨域问题,不然谁会没事用这种格式. 下面是我用JSONP的一些心得体会: JSONP是JSON with Padding的略称.它是一个非官方的协议,它允许在服务器端集成S ...
- java XML 通过BeanUtils的population为对象赋值 根据用户选择进行dom4j解析
根据xml文件设计Student对象 <?xml version="1.0" encoding="UTF-8"?> <students> ...
- Objection, 一个轻量级的Objective-C依赖注入框架
简介 项目主页:https://github.com/atomicobject/objection 实例下载: https://github.com/ios122/ios122 Objection 是 ...
- iOS MapKit地图
地图框架:#import <MapKit/MapKit.h> 基本属性和方法: 属性: 地图类视图:MKMapView 地图类型:MKMapType mapType 地图旋转:rotate ...