LintCode_133 最长单词
题目
给一个词典,找出其中所有最长的单词。
样例
在词典
{
"dog",
"google",
"facebook",
"internationalization",
"blabla"
}
中, 最长的单词集合为 ["internationalization"]
在词典
{
"like",
"love",
"hate",
"yes"
}
中,最长的单词集合为 ["like", "love", "hate"]
思路
用动态规划的思路解决
vector<string> s;
假设我已经求出了前i个单词的最长集合S(i);
用L(i)表示最长集合S(i)的单词长度;
求S(i + 1)则分为三种情况
if(L(i) > dictionary[i + 1].length) S(i + 1) = S(i);
else if(L(i) == dictionary[i + 1].length) S(i + 1) = S(i) + dictionary[i + 1];
else if(L(i) < dictionary[i + 1].length)
{
S(i + 1) = dictionary[i + 1];
}
C++代码
vector<string> longestWords(vector<string> &dictionary) {
// write your code here
vector<string> s;
int i;
s.push_back(dictionary[0]);
int t;
for(i = 1; i < dictionary.size(); ++i)
{
if(s[0].length() == dictionary[i].length()) s.push_back(dictionary[i]);
else if(s[0].length() < dictionary[i].length())
{
s.clear();
s.push_back(dictionary[i]);
}
}
return s;
}
LintCode_133 最长单词的更多相关文章
- OpenJudge就算概论-最长单词2【寻找句子内部最长的单词】
/*===================================== 最长单词2 总时间限制: 1000ms 内存限制: 65536kB 描述 一个以'.'结尾的简单英文句子,单词之间用空格 ...
- lintcode :最长单词
题目: 最长单词 给一个词典,找出其中所有最长的单词. 样例 在词典 { "dog", "google", "facebook", &quo ...
- 英文长单词断行 word-break VS word-wrap
你真的了解word-wrap和word-break的区别吗? 这两个东西是什么,我相信至今还有很多人搞不清,只会死记硬背的写一个word-wrap:break-word;word-break:brea ...
- CSS3让长单词与URL地址自动换行——word-wrap属性
div{ word-wrap:break-word; } word-wrap属性可以使用的属性值为normal与break-word两个.使用normal属性值时浏览器默认处理,只在半角空格或者连字符 ...
- [LeetCode] Longest Word in Dictionary 字典中的最长单词
Given a list of strings words representing an English Dictionary, find the longest word in words tha ...
- 允许长单词、数字、URL换行到下一行
CSS3 word-wrap 属性 normal 只在允许的断字点换行(浏览器保持默认处理) break-word 在长单词.数字.URL地址内部进行换行 页面效果图: 源码:
- C语言 · 最长单词
算法提高 最长单词 时间限制:1.0s 内存限制:512.0MB 编写一个函数,输入一行字符,将此字符串中最长的单词输出. 输入仅一行,多个单词,每个单词间用一个空格隔开.单词仅由小 ...
- lintcode-133-最长单词
133-最长单词 给一个词典,找出其中所有最长的单词. 样例 在词典 { "dog", "google", "facebook", &quo ...
- word-wrap与word-break为长单词换行
如果你遇到长串英文单词或者url换行的问题,这时候就需要用到word-wrap与word-break这2个css属性啦. word-wrap:break-word;长单词与url地址自动换行. wor ...
随机推荐
- axis2开发webservice接口入门到精通详解(转)
最近在开发接口,在网上发现了两篇不错的文章,给大家分享下: 第一篇: 一.Axis2的下载和安装 1.可从http://ws.apache.org/axis2/ 下载Axis2的最新版本: ...
- PostgreSQL的基础数据类型分析记录-转
src:http://www.codeweblog.com/postgresql%E7%9A%84%E5%9F%BA%E7%A1%80%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E ...
- 基于baseline和stochastic gradient descent的个性化推荐系统
文章主要介绍的是koren 08年发的论文[1], 2.1 部分内容(其余部分会陆续补充上来). koren论文中用到netflix 数据集, 过于大, 在普通的pc机上运行时间很长很长.考虑到写文 ...
- 牛客NOIP暑期七天营-TG1 赛后题解
目录 牛客NOIP暑期七天营-提高组1 A-最短路 题目描述 link 题解 代码 B-最小生成链 题目描述 link 题解 代码 C-最小字典最短路 题目描述 link 题解 Update 牛客NO ...
- 一些常见的synthesis attribute
Noprune A Verilog HDL synthesis attribute that prevents the Quartus II software from removing a regi ...
- 使用DynamoShake从dynamodb迁移到mongodb
去年和今年年初,我们开源了MongoShake和RedisShake分别用于MongoDB和Redis的迁移.同步.备份等多种需求.最近,我们的shake系列又进一步壮大,我们推出了一款dynamod ...
- string主要操作函数
参考博客:http://blog.csdn.net/zhenyusoso/article/details/7286456 std::string illegal(" \t\f\v\n\r\\ ...
- sas单变量的特征分析
sas单变量的特征分析 大炮,我有个烦恼,我领导最近老叫我单变量结合因变量分析,但是都是分段分析,我总是写proc sql然后group by ,但是这个过程好无聊啊,有木有什么新的代码,让我可以分析 ...
- VitualBox虚拟机安装CentOS, shell模式与图形化界面的相互切换
方法一:永久切换 # vi /etc/inittab 编辑 init 5 为 init 3,重启就自动进入控制台方式:反之桌面模式 方法二:当前有效 桌面模式切换shell模式:Ctrl + Alt ...
- PAT甲级——A1062 Talent and Virtue
About 900 years ago, a Chinese philosopher Sima Guang wrote a history book in which he talked about ...