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的更多相关文章

  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目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  5. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  6. 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 ...

  7. 《Cracking the Coding Interview》——第18章:难题——题目13

    2014-04-29 04:40 题目:给定一个字母组成的矩阵,和一个包含一堆单词的词典.请从矩阵中找出一个最大的子矩阵,使得从左到右每一行,从上到下每一列组成的单词都包含在词典中. 解法:O(n^3 ...

  8. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

  9. 《Cracking the Coding Interview》——第17章:普通题——题目13

    2014-04-29 00:15 题目:将二叉搜索树展开成一个双向链表,要求这个链表仍是有序的,而且不能另外分配对象,就地完成. 解法:Leetcode上也有,递归解法. 代码: // 17.13 F ...

随机推荐

  1. session的MaxInactiveInterval=0在tomcat6和tomcat8不同表现

    在tomcat6中调用 request.getSession().setMaxInactiveInterval(0); 这个session会立即过期. 而在tomcat8中,同样的调用,会导致这个se ...

  2. java注解总结-关联信息-关联结构

    java的注解是一种可配置信息: 这些信息直接依附在功能代码之上: * 元注解@Target,@Retention,@Documented,@Inherited * * @Target 表示该注解用于 ...

  3. Nodejs事件监听模块

    nodejs里面是不存在浏览器里面都冒泡,捕获这些行为的,所以Nodejs实现了events这个模块,里面大多数的模块都集成了这个模块,所以events是node模块里面最重要都一个模块,他对外只暴露 ...

  4. 2018.8.19 mybatis 环境搭建---配置mysql 。(Windows环境下面)

    安装mysql Install/Remove of the Service Denied!错误的解决办法 在windos 的cmd下安装mysql 在mysql的bin目录下面执行: mysqld - ...

  5. Spring boot 项目导出可执行jar

    配置文件中添加插件 <plugin> <groupId>org.springframework.boot</groupId> <artifactId>s ...

  6. Python 二进制 八进制 十进制 十六进制

    1.四种进制的表示方式 >>> 0b010 0b二进制 >>> 0x010 0x 十六进制 >>> 0o010 0o 八进制 >>&g ...

  7. poj_1845_Sumdiv

    Consider two natural numbers A and B. Let S be the sum of all natural divisors of A^B. Determine S m ...

  8. 基于mybatis设计简单信息管理系统---jsp页面

    1.在设计编辑界面的时候需要有一个下拉的列表页,想要他指定到指定的值: <select id="categoryId" name="categoryId" ...

  9. 《Redis设计与实现》- 复制

    在分布式系统中为了解决单点问题,通常会把数据复制多个副本部署到其他机器,满足故障恢复和负载均衡灯需求.Redis提供了复制功能,实现了相同数据多个副本,复制功能作是高可用Redis的基础,深入理解复制 ...

  10. Oauth2.0协议 http://www.php20.com/forum.php?mod=viewthread&tid=28 (出处: 码农之家)

    概要     OAuth2.0是OAuth协议的下一版本,但不向后兼容OAuth 1.0即完全废止了OAuth1.0. OAuth 2.0关注客户端开发者的简易性.要么通过组织在资源拥有者和HTTP服 ...