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. Python开发第五篇

    面向对象程序设计 面向过程编程:就是分析问题的解决步骤,按部就班的编写代码解决问题 函数式编程:就是把代码封装到函数中,然后在使用时调用封装好的函数 面向对象编程:把一类事物所共有的属性和行为提取出来 ...

  2. QT5中两个窗体之间传递信息(值)

    一个窗体A调用另一个窗体B: 1)包含窗体B的头文件#include"B.h" 2)在窗体A中增加slots函数: public slots: void infoRecv(QStr ...

  3. NYOJ(325)+NYOJ(456),01背包

    题目链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=325 http://acm.nyist.net/JudgeOnline/problem. ...

  4. 20145238-荆玉茗 《Java程序设计》第二周学习总结

    20145238 <Java程序设计>第2周学习总结 教材学习内容总结 关于一些格式方面的问题: 1.关键字:在定义java文件名的时候要避免这些关键字的出现,因为他们在java程序语言中 ...

  5. fast rcnn的实例

    http://www.cnblogs.com/louyihang-loves-baiyan/p/4906690.html https://saicoco.github.io/object-detect ...

  6. Matlab将矩阵保存为图像

    imwrite(image,'image.jpg'); image为矩阵的内容 image.jpg为要保存的图像的名字

  7. 【洛谷P1090】合并果子

    合并果子 题目链接 贪心:每次先合并最小的两堆果子 用堆实现 #include<iostream> #include<cstdio> using namespace std; ...

  8. c/c++基础

    如果有你认为重要的知识点,而我这却没有记录下来的,那么期待你分享给我(^U^)ノ~YO. 1.在结构体中,符号->的前面是指针变量,符号.的前面是普通变量.   程序中a->b等价于(*a ...

  9. 【iOS】史上最全的iOS持续集成教程 (下)

    :first-child{margin-top:0!important}.markdown-body>:last-child{margin-bottom:0!important}.markdow ...

  10. 牛客小白月赛1 I あなたの蛙が帰っています 【卡特兰数】

    链接:https://www.nowcoder.com/acm/contest/85/I题目描述 あなたの蛙が帰っています!  蛙蛙完成了一趟旅行,回家啦!但它还是没有去它心中非常想去的几个地方.总共 ...