《Cracking the Coding Interview》——第18章:难题——题目7
2014-04-29 03:05
题目:给定一个词典,其中某些词可能能够通过词典里其他的词拼接而成。找出这样的组合词里最长的一个。
解法:Leetcode上有Word Break这道题,和这题基本思路一致。
代码:
// 18.7 Given a list of words, find out the longest word made of other words in the list.
#include <iostream>
#include <string>
#include <unordered_set>
#include <vector>
using namespace std; class Solution {
public:
string longestBreakableWord(unordered_set<string> &dict) {
unordered_set<string>::const_iterator usit;
string res = ""; for (usit = dict.begin(); usit != dict.end(); ++usit) {
if (wordBreak(*usit, dict) && usit->length() > res.length()) {
res = *usit;
}
} return res;
}
private:
bool wordBreak(string s, unordered_set<string> &dict) {
int n;
int i, j;
string str;
vector<int> dp; n = (int)s.length();
if (n == || dict.empty()) {
return false;
}
dp.resize(n);
for (i = ; i < n; ++i) {
str = s.substr(, i + );
if (dict.find(str) != dict.end()) {
dp[i] = ;
} else {
for (j = ; j < i; ++j) {
if (dp[j] && dict.find(s.substr(j + , i - j)) != dict.end()) {
dp[i] = ;
break;
}
}
if (j == i) {
dp[i] = ;
}
}
} i = dp[n - ];
dp.clear();
return i == ;
}
}; int main()
{
unordered_set<string> dict;
string s;
Solution sol;
int i, n; while (cin >> n && n > ) {
for (i = ; i < n; ++i) {
cin >> s;
dict.insert(s);
} cout << sol.longestBreakableWord(dict) << endl;
} return ;
}
《Cracking the Coding Interview》——第18章:难题——题目7的更多相关文章
- 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(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- 《Cracking the Coding Interview》——第18章:难题——题目13
2014-04-29 04:40 题目:给定一个字母组成的矩阵,和一个包含一堆单词的词典.请从矩阵中找出一个最大的子矩阵,使得从左到右每一行,从上到下每一列组成的单词都包含在词典中. 解法:O(n^3 ...
- 《Cracking the Coding Interview》——第18章:难题——题目12
2014-04-29 04:36 题目:最大子数组和的二位扩展:最大子矩阵和. 解法:一个维度上进行枚举,复杂度O(n^2):另一个维度执行最大子数组和算法,复杂度O(n).总体时间复杂度为O(n^3 ...
- 《Cracking the Coding Interview》——第18章:难题——题目11
2014-04-29 04:30 题目:给定一个由‘0’或者‘1’构成的二维数组,找出一个四条边全部由‘1’构成的正方形(矩形中间可以有‘0’),使得矩形面积最大. 解法:用动态规划思想,记录二维数组 ...
随机推荐
- java日期时间Date类
java.util包提供了Date类来封装当前的日期和时间. Date类提供两个构造函数来实例化Date对象. 第一个构造函数使用当前日期和时间来初始化对象. Date( ) 第二个构造函数接收一个参 ...
- Poj(1521),哈夫曼编码
题目链接:http://poj.org/problem?id=1521 这里,网上有很多博客都有写,很多人没有建树,直接就是求一下这个哈夫曼编码的长度,的确很巧妙,我也用的这个方法,但是,几乎所有博客 ...
- 学习MyBatis之简单入门HelloWorld
转:https://blog.csdn.net/gaomb_1990/article/details/78299784 一.准备 Eclipse:Luna Service Release 1 (4.4 ...
- PHP 5.4 on CentOS/RHEL 7.0, 6.5 and 5.10 via Yum
PHP 5.4.36 has been released on PHP.net on 18th December 2014, and is also available for CentOS/RHEL ...
- php无法保存SESSION问题总汇
昨天客户又过来说网站的问题,说的也都是些毛毛雨的东西,管理那么多网站,再有这么些客户的存在,本人也是累了,但当登录后台的时候突然发现后台登录不了,查看了一下验证码服务器端的session为空值,之前登 ...
- C++STL之vector向量容器
vector向量容器 vector向量容器不但能向数组一样对元素进行随机访问, 还能在尾部插入元素 vector具有内存自动管理的功能, 对于元素的插入和删除, 可动态调整所占的内存空间 vect ...
- Java中阻塞队列的使用
http://blog.csdn.net/qq_35101189/article/details/56008342 在新增的Concurrent包中,BlockingQueue很好的解决了多线程中,如 ...
- cudaMallocPitch()
- C语言学生成绩管理系统(简易版)
#include<stdio.h> #include<stdlib.h> #include<string.h> int readstudents(struct st ...
- 概述「DAG加边至强连通」模型&&luoguP2746校园网Network of Schools
模型概述 有一DAG,问最少加多少条边能够使图强连通. 题目描述 一些学校连入一个电脑网络.那些学校已订立了协议:每个学校都会给其它的一些学校分发软件(称作“接受学校”).注意即使 B 在 A 学校的 ...