5.7 Given a list of words, write a program to find the longest word made of other words in the list.

这道题给了我们一个字符串数组,让我们找到最长的那个单词是由字符串数组中的其他单词组成的,LeetCode上跟类似的题目有Word BreakWord Break II。那么我们首先来想如果是拆分两个单词怎么做,那我们要首先把所有的单词存到哈希表里,然后遍历每个单词,每个位置上都拆分成左右两个字符串,然后看它们是否都在哈希表中存在,都存在的话就表示该单词符合要求。那么对于拆分成多个单词,我可以使用递归来做,我们首先给单词组进行排序,长度长的在前面,我们需要用哈希表建立单词和其是否能拆分的布尔值之间的映射,还有用一个变量is_original_word表示该单词是单词组的单词还是在递归过程中拆分出来的单词,然后从头开始遍历单词,对于每个单词,我们还是从每个位置拆分成左右两边,如果左边的单词在哈希表中存在且其是可以拆分的,那么我们再递归调用右边的单词,如果所有的拆分方法都完成了,该单词还是不能拆成已有的单词,那么将其哈希表中的值赋为false。

bool can_build_word(string word, bool is_original_word, unordered_map<string, bool> &m) {
if (m.count(word) && !is_original_word) return m[word];
for (int i = ; i < word.size(); ++i) {
string left = word.substr(, i);
string right = word.substr(i);
if (m.count(left) && m[left] && can_build_word(right, false, m)) {
return true;
}
}
m[word] = false;
return false;
} string print_longest_word(vector<string> &words) {
unordered_map<string, bool> m;
for (auto a : words) m[a] = true;
sort(words.begin(), words.end(), [](const string &a, const string b){return a.size() > b.size();});
for (auto a : words) {
if (can_build_word(a, true, m)) {
return a;
}
}
return "";
}

CareerCup All in One 题目汇总

[CareerCup] 18.7 Longest Word 最长的单词的更多相关文章

  1. [LeetCode] Longest Word in Dictionary 字典中的最长单词

    Given a list of strings words representing an English Dictionary, find the longest word in words tha ...

  2. [LeetCode] Longest Word in Dictionary through Deleting 删除后得到的字典中的最长单词

    Given a string and a string dictionary, find the longest string in the dictionary that can be formed ...

  3. [Swift]LeetCode524. 通过删除字母匹配到字典里最长单词 | Longest Word in Dictionary through Deleting

    Given a string and a string dictionary, find the longest string in the dictionary that can be formed ...

  4. [Swift]LeetCode720. 词典中最长的单词 | Longest Word in Dictionary

    Given a list of strings words representing an English Dictionary, find the longest word in words tha ...

  5. LeetCode 720. Longest Word in Dictionary (字典里最长的单词)

    Given a list of strings words representing an English Dictionary, find the longest word in words tha ...

  6. C#LeetCode刷题之#720-词典中最长的单词(Longest Word in Dictionary)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4120 访问. 给出一个字符串数组words组成的一本英语词典.从 ...

  7. 524. Longest Word in Dictionary through Deleting【Medium】【删除后得到的字典中的最长单词】

    Given a string and a string dictionary, find the longest string in the dictionary that can be formed ...

  8. 720. Longest Word in Dictionary 能连续拼接出来的最长单词

    [抄题]: Given a list of strings words representing an English Dictionary, find the longest word in wor ...

  9. FCC JS基础算法题(3):Find the Longest Word in a String (找出最长单词)

    题目描述: 在句子中找出最长的单词,并返回它的长度.函数的返回值应该是一个数字. 基本思路,将字符串转换成数组,然后得出数组中单个元素的长度,对长度进行排序,返回最大的一个 代码: function ...

随机推荐

  1. 开始我的PostgreSQL的学习之旅

    经过这么长时间的学习,终于确定了我的研究方向是PostgreSQL的空间数据库的设计流程,具体怎样实现这个过程,其难度是挺大的,我必须克服掉,尽量得往前看.大家有相同的研究方向的,可以一同来学习,相互 ...

  2. loadrunner通过C语言实现字符的替换(只能替换单个字符,慎用)

    如果按照普通的定义字符串就会出现以下错误: 解决方法如下: 将双引号改成单引号: lr_searchReplace(abc,"test",' ','+'); Action也可以这些 ...

  3. asp.net项目中通过Web.config配置文件及文件夹的访问权限!

    描述:在开发中我们通常会碰到这样的问题,例如:在项目的根目录下面有一个文件或者文件夹需要用户登陆后才能访问.如果用户在没有登录的情况下访问该文件或者该文件夹下面的文件时,直接拦截重定向到对应的登陆页面 ...

  4. MFC 打开文件夹 调用其他程序 打开文件

    ShellExecute(NULL,TEXT("OPEN"),要打开的文件的路径,NULL,NULL,SW_SHOWNORMAL); ShellExecute(NULL, &quo ...

  5. node.js第一次

    随着时代的变迁,日月星辰轮回,不断的有新的事物被创造于世,作为在这个世界活着的前端工程崽的我,最近又接触了一门新手艺“node.js”.自从它2009年诞生至今,被很多前端推崇,我起步已经晚了,还好它 ...

  6. javaScript封装的各种写法

    在javascript的世界里,写法是个神奇的现象,真是百家齐开放啊!每次看到老外写的js组件,思想和写法都怪异,就没看到一个js结构基本相同的代码出来.今天,我就来谈谈js写法,我在开发过程中,也写 ...

  7. java常用

    文件读写几种形式 http://www.cnblogs.com/qianbi/p/3378466.html java的get post请求 http://www.cnblogs.com/zhuawan ...

  8. ural 2067. Friends and Berries

    2067. Friends and Berries Time limit: 2.0 secondMemory limit: 64 MB There is a group of n children. ...

  9. HDU 3333 & 离线+线段树

    题意: 统计一段区间内不同数字之和.如1 1 2 3 1 统计2---5即1+2+3. SOL: 很少打过离线的题目...这种可离线可在线的题不管怎么样一般都是强行在线... 考虑这题,此前做过一个类 ...

  10. Android 情景模式设置

    情景模式的设置大家应当相当熟悉了,但是在Android中如何通过自己的程序进行情景模式的设置呢,情景模式分为多种多种,即可以使用系统自带的,也可 以使用自定义的,但是在开发某些程序时,可能需要在程序中 ...