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

  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(CC150第五版)

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

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

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

  9. 《Cracking the Coding Interview》——第18章:难题——题目12

    2014-04-29 04:36 题目:最大子数组和的二位扩展:最大子矩阵和. 解法:一个维度上进行枚举,复杂度O(n^2):另一个维度执行最大子数组和算法,复杂度O(n).总体时间复杂度为O(n^3 ...

  10. 《Cracking the Coding Interview》——第18章:难题——题目11

    2014-04-29 04:30 题目:给定一个由‘0’或者‘1’构成的二维数组,找出一个四条边全部由‘1’构成的正方形(矩形中间可以有‘0’),使得矩形面积最大. 解法:用动态规划思想,记录二维数组 ...

随机推荐

  1. anaconda添加源(channels)

    如果使用environment.yml下载conda环境时,系统很慢,那么可以增加其他的下载源: - https://conda.anaconda.org/menpo - https://mirror ...

  2. 双击易语言没有反应,按住shift再双击可解决

    参考资料:http://tieba.baidu.com/p/2987732743 的7楼.

  3. QR分解与最小二乘(转载自AndyJee)

    转载网址:http://www.cnblogs.com/AndyJee/p/3846455.html 主要内容: 1.QR分解定义 2.QR分解求法 3.QR分解与最小二乘 4.Matlab实现 一. ...

  4. Linux ELF格式分析

    http://www.cnblogs.com/hzl6255/p/3312262.html ELF, Executable and Linking Format, 是一种用于可执行文件.目标文件.共享 ...

  5. Poj(2312),坦克大战,BFS的变形

    题目链接:http://poj.org/problem?id=2312 挺有趣的一道题目,然而很容易WA,我就WA了一次,虽然我Debug的时候已经知道哪里出问题了,就是比如说我搜到B和E时,从B搜第 ...

  6. 剑指offer39 平衡二叉树

    剑指上用了指针传递,这里用的引用传递 class Solution { public: bool IsBalanced_Solution(TreeNode* pRoot) { ; return IsB ...

  7. C# 操作符与表达式

    C#保留了C++所有的操作符,其中指针操作符(*和->)与引用操作符(&)需要有unsafe的上下文.C#摈弃了范围辨析操作符(::),一律改为单点操作符(.).我们不再阐述那些保留的C ...

  8. Apache 负载均衡 端口转发 配置

    转载自:https://blog.csdn.net/snihcel/article/details/38844323 [端口转发配置]       通过http_proxy做tomcat的端口转发: ...

  9. CharSquence 接口的作用,多态以增强String

    CharSquence 接口的作用,多态以增强String,CharSquence 可以多态定义 String StringBuffer StringBuilder.

  10. Delphi7程序调用C#写的DLL解决办法(转)

    近来,因工作需要,必须解决Delphi7写的主程序调用C#写的dll的问题.在网上一番搜索,又经过种种试验,最终证明有以下两种方法可行:    编写C#dll的方法都一样,首先在vs2005中创建一个 ...