720. Longest Word in Dictionary

本文介绍了一种算法,用于从给定的英文词汇列表中找出能够由其他单词逐字符构建的最长单词,并按字典序返回。通过排序与哈希集合的结合使用,实现了高效的查找。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1,题目要求
Given a list of strings words representing an English Dictionary, find the longest word in words that can be built one character at a time by other words in words. If there is more than one possible answer, return the longest word with the smallest lexicographical order.

If there is no answer, return the empty string.
这里写图片描述
以字典序,找到可以组成的最长的字符串。

2,题目思路
因为给定的字符串序列是无序的,因此应该首先对整个字符串向量进行排序,这样题目的要求就基本完成了一办了。其次就是依次对字符串进行判断,看是否满足题设条件。
首先,如果该字符串的长度为1,或者该字符串的前n-1位在设定的set中出现过,那么就继续进行判断。
如果该字符串的长度比准备返回的字符串res长度要长,则对准备返回的字符串进行更新操作。

3,程序源码

class Solution {
public:
    string longestWord(vector<string>& words) {
        sort(words.begin(),words.end());    //按照字母顺序对向量里的元素进行排序
        unordered_set<string> record;
        string res;
        for(string word : words)
        {
            if(word.size()==1 || record.count(word.substr(0,word.size()-1)))//长度为1或者含有长度为n-1子串的成员在set之中
            {
                if(word.size()>res.size())  //长度是不是变长了?
                    res = word;
                record.insert(word);
            }
        }
        return res;

    }
};

首先,sort函数的排序是按照字典序进行排列的。
其次,因为要进行存在性的查询,因此要用到集合的特性,而且用到的是特殊的一种集合形式:
unordered_set
1,头文件为< unorder_set>
2,其次,该容器与普通set最大的不同就是,其中的元素是无序的,因此特点就是如果仅仅需要对元素值进行查询,速度上会快许多。

老师教学上字典和元组是这样的“在需要使⽤序列的情况下: • 函数的返回值 • 字典的键值 • 函数的参数 directory[last, first] = number • for last, first in directory: • print(first, last, directory[last,first]) def has_match(t1, t2): • for x, y in zip(t1, t2): • if x == y: • return True • return False • for index, element in enumerate('abc'): • print(index, element)请你基于老师教学的代码完成练习12-2 “Exercise 12-2. More anagrams! 148 | Chapter 12: Tuples 1. Write a program that reads a word list from a file (see "Reading Word Lists" on page 99) and prints all the sets of words that are anagrams. Here is an example of what the output might look like: ['deltas', 'desalt', 'lasted', 'salted', 'slated', 'staled'] ['retainers' ['renerating greatening , 'greatening'] ['resmelts' , 'smelters' , 'termless'] Hint: you might want to build a dictionary that maps from a collection of letters to a list of words that can be spelled with those letters. The question is, how can you represent the collection of letters in a way that can be used as a key? 2. Modify the previous program so that it prints the longest list of anagrams first, followed by the second longest, and so on. 3. In Scrabble, a "bingo" is when you play all seven tiles in your rack, along with a letter on the board, to form an eight-letter word. What collection of eight letters forms the most possible bingos? Hint: there are seven.”请你运用python运行规则 编写练习12-2要求的运行代码 注意代码编写具有可读性和完整性 能够真实运行出符合要求的数据 注意分两版答案 第一版应该有注释 第二版为纯代码 其中题目有说明解题的思路 可以按照题目的思路步骤一步一步完成
最新发布
06-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值