[CareerCup] 18.7 Longest Word 最长的单词
5.7 Given a list of words, write a program to find the longest word made of other words in the list.
这道题给了我们一个字符串数组,让我们找到最长的那个单词是由字符串数组中的其他单词组成的,LeetCode上跟类似的题目有Word Break和Word 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] 18.7 Longest Word 最长的单词的更多相关文章
- [LeetCode] Longest Word in Dictionary 字典中的最长单词
Given a list of strings words representing an English Dictionary, find the longest word in words tha ...
- [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 ...
- [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 ...
- [Swift]LeetCode720. 词典中最长的单词 | Longest Word in Dictionary
Given a list of strings words representing an English Dictionary, find the longest word in words tha ...
- LeetCode 720. Longest Word in Dictionary (字典里最长的单词)
Given a list of strings words representing an English Dictionary, find the longest word in words tha ...
- C#LeetCode刷题之#720-词典中最长的单词(Longest Word in Dictionary)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4120 访问. 给出一个字符串数组words组成的一本英语词典.从 ...
- 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 ...
- 720. Longest Word in Dictionary 能连续拼接出来的最长单词
[抄题]: Given a list of strings words representing an English Dictionary, find the longest word in wor ...
- FCC JS基础算法题(3):Find the Longest Word in a String (找出最长单词)
题目描述: 在句子中找出最长的单词,并返回它的长度.函数的返回值应该是一个数字. 基本思路,将字符串转换成数组,然后得出数组中单个元素的长度,对长度进行排序,返回最大的一个 代码: function ...
随机推荐
- Web分布式架构演变过程
1 单台服务器 2 应用服务器与数据库服务器 文件服务器分开 3 数据库 缓存 读写分离 实时写 非实时写 4 应用服务器集群 需要再配一台”负载均衡调度器“,nginx 5 数据库拆封 主库 ...
- AIX RAC ORA-27504 ORA-27300 ORA-27301 ORA-27302 ORA-27303
操作系统:AIX6.1 数据库:Oracle10.2.0.5 RAC 2个节点.其中一个节点正常,另外一个节点的instance的状态是offline的,手工去启动的时候报错: ORA: IPC e ...
- android 消息推送
android 消息推送 极光推送百度云推送(语音)友盟消息推送
- cocos2dx游戏开发——别踩白块学习笔记(一)——Block类
一.Block类介绍 当然啦,Block类在这个游戏里就是必需品= =,因为整体都是由这个搞出来的,所以我们可以把游戏需要实现的功能都放在这里. 主要有下面这些功能(经典模式): 1.创建一个Bloc ...
- ERROR Shell: Failed to locate the winutils binary in the hadoop binary path
文章发自:http://www.cnblogs.com/hark0623/p/4170172.html 转发请注明 14/12/17 19:18:53 ERROR Shell: Failed to ...
- LoadRunner11录制APP脚本(2)
通过安卓模拟器实现LoadRunner11录制APP脚本 http://www.51testing.com/html/24/15110424-3686857.html http://www.51tes ...
- XSS攻击&SQL注入攻击&CSRF攻击?
- XSS(Cross Site Script,跨站脚本攻击)是向网页中注入恶意脚本在用户浏览网页时在用户浏览器中执行恶意脚本的攻击方式.跨站脚本攻击分有两种形式:反射型攻击(诱使用户点击一个嵌入恶意 ...
- poj1745 dp
题目链接:http://poj.org/problem?id=1745 类似的题目之前写过一个差不多的(链接:http://www.cnblogs.com/a-clown/p/5982611.html ...
- STL(multiset) UVA 11020 Efficient Solutions
题目传送门 题意:训练指南P228 分析:照着书上的做法,把点插入后把它后面不占优势的点删除,S.size ()就是优势的人数,时间复杂度O (nlogn) #include <bits/std ...
- Python基础5- 运算符
Python的运算符和其他语言的类似,主要有:算术运算符.比较运算符.逻辑运算符.赋值运算符.成员运算符.位运算符 ----------------------------------------算术 ...