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;
}

  

时间: 2024-08-02 15:51:10

LintCode_133 最长单词的相关文章

(算法)最长单词

题目: 给定一组单词,找出其中的最长单词,且该单词由这组单词中的其他单词组成. 思路: 首选将单词按照字符串大小从大到小排序,然后依次判断该单词是否由其他单词组成. 将单词保存在散列表中,这样容易查找. 判断单词组成:依次切分为左右两个字符串,然后递归查找判断.(为避免重复计算,在每一次递归中都保存中间结果,即把是否可以组成单词的结果都保存在散列表中) 代码: #include<iostream> #include<vector> #include<map> #incl

C语言 &#183; 最长单词

算法提高 最长单词 时间限制:1.0s   内存限制:512.0MB 编写一个函数,输入一行字符,将此字符串中最长的单词输出. 输入仅一行,多个单词,每个单词间用一个空格隔开.单词仅由小写字母组成.所有单词的长度和不超过100000.如有多个最长单词,输出最先出现的. 样例输入 I am a student 样例输出 student 1 #include<stdio.h> 2 #include<string.h> 3 #define max 100000 4 int is_zimu

lintcode 容易题:Longest Words 最长单词

题目: 最长单词 给一个词典,找出其中所有最长的单词. 样例 在词典 { "dog", "google", "facebook", "internationalization", "blabla" } 中, 最长的单词集合为 ["internationalization"] 在词典 { "like", "love", "hate"

CSS3让长单词与URL地址自动换行——word-wrap属性

div{ word-wrap:break-word; } word-wrap属性可以使用的属性值为normal与break-word两个.使用normal属性值时浏览器默认处理,只在半角空格或者连字符的地方进行换行.使用break-word时浏览器可在长单词或URL地址内部进行换行. 目前,word-wrap属性得到了所有浏览器的支持.

英文长单词断行 word-break VS word-wrap

你真的了解word-wrap和word-break的区别吗? 这两个东西是什么,我相信至今还有很多人搞不清,只会死记硬背的写一个word-wrap:break-word;word-break:break-all;这样的东西来强制断句,又或者是因为这两个东西实在是太拗口了,长得又差不多,导致连背都很难背下来. 那它们到底是什么呢?我在mozilla的官网上找到如下的解释: word-wrap word-break 我们看到两个解释中都出现了 break lines within words 这样的

【北航软件工程】最长单词链

Part.1 github链接 Part.2 PSP2.1 Personal Software Process Stages 预计耗时(分钟) 实际耗时(分钟) Planning 计划 15 10 PSP2.1 估计这个任务需要多少时间 15 10 Development 计划 1120 1360 · Analysis 需求分析 (包括学习新技术) 120 150 · Design Spec 生成设计文档 30 30 · Design Review 设计文档复审 10 10 · Coding S

返回最长单词的字母个数

返回最长单词的字母个数 Javascript 这种写法太清晰了. 使用了 Math的 max. 用了 Array 的 map 迭代. 用了回调. 用了字符串的分隔. return Math.max.apply(Math, str.split(" ").map(function(el) { return el.length;})); 原文地址:https://www.cnblogs.com/F4NNIU/p/10677107.html

[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 by deleting some characters of the given string. If there are more than one possible results, return the longest word with the smallest lexicographic

来自百度贴吧的练习题 :求最长单词的长度和最短单词的长度。

In the function ex5 write code that will input a line of text, split it into words, and display these words one per line, and also print the length of the longest and shortest words. You should regard any sequence of consecutive non-space characters