zoukankan      html  css  js  c++  java
  • 720. Longest Word in Dictionary--easy

    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.
    Example 1:
    Input:
    words = ["w","wo","wor","worl", "world"]
    Output: "world"
    Explanation:
    The word "world" can be built one character at a time by "w", "wo", "wor", and "worl".
    Example 2:
    Input:
    words = ["a", "banana", "app", "appl", "ap", "apply", "apple"]
    Output: "apple"
    Explanation:
    Both "apply" and "apple" can be built from other words in the dictionary. However, "apple" is lexicographically smaller than "apply".
    Note:

    All the strings in the input will only contain lowercase letters.
    The length of words will be in the range [1, 1000].
    The length of words[i] will be in the range [1, 30].

    1.思考

    • 一开始就想到要对输入的words进行排序,按长度从小到大排序,且相等长度按照字母从小到大排序。下面写了comp函数,后来发现其实直接sort(words.begin(), words.end())就可以实现前面的排序的;
    • 然后最先想到的搜索方式存在漏洞,eg. ["rac","rs","ra","on","r","otif","o","onpdu","rsf","rs","ot","oti","racy","onpd"]中,就只能找到on,后来发现应该是同样长度的没有全部遍历,于是就修改为DPS了;
    • 用DPS之后那么整个的复杂度就上升了,运行速度就下降了。后来还优化成只从当前单词所在位置之后开始搜索,且只搜索比当前长度大1的单词;
    • 相比REF中的方法,使用unordered_set <string>的方法,就减少了重复搜索的过程,降低了运行时间,后面附上REF;
    • 如果没有想到简便的方法,一点也不easy……

    2.知识点

    • DFS;
    • string比较大小可直接用>、<、==;
    • 截取string中一部分,可用substr函数,eg. s.substr(1,3)//1-3; s.substr(3)//0-3。

    3.实现
    Runtime: 664ms( 5.19% )——REF:72ms
    Memory: 91.4MB( 5.55% )——REF:18.5MB

    class Solution {
    public:
        string longestWord(vector<string>& words) {
            sort(words.begin(), words.end(), comp);
            string res, ss, st;
            int len = words.size();
            for(int i=0; i<len; i++){
                st = words[i];
                if(st.size()==1){
                    ss = DPSFindWord(words, st, i+1);
                    if(res.size()<ss.size() || //longer word
                      (res.size()==ss.size() && res>ss))//smallest lexicographical order
                        res = ss;
                }
                else
                    break;
            }      
            return res;
        }
        
        string DPSFindWord(vector<string>& words, string s, int pos)
        {
            string res = s, ss, st;
            int len = words.size();
            for(int i=pos; i<len; i++){
                st = words[i];
                if(s.size()+1==st.size()){
                    if(s==st.substr(0, st.size()-1)){
                        ss = DPSFindWord(words, st, i+1);
                        if(res.size()<ss.size() ||
                          (res.size()==ss.size() && res>ss))
                            res = ss;
                    }
                }
                else if(s.size()+1<st.size())
                    break;
            }
            return res;
        }    
        
        static bool comp(const string a, const string b)
        {
            if(a.size()<b.size())
                return true;
            else if(a.size()==b.size())//smallest lexicographical order
                return a<b;
            else
                return false;
        }
    };
    
    //REF
    class Solution {
    public:
        string longestWord(vector<string>& words) {
            sort(words.begin(), words.end());
            string res;
            unordered_set<string> mp;
            
            for(auto st:words){
                if(st.size()==1 || mp.count(st.substr(0, st.size()-1))){
                    res = st.size()>res.size()?st:res;
                    mp.insert(st);
                }
            }
            return res;
        }    
    };
    
  • 相关阅读:
    波形捕捉:(9)写入到WAV文件
    C#基础回顾:GridView全选演示
    VS.net和Reflector 图标解释
    Dot Net屏幕传输 v1.0
    C#基础回顾:用GDI+绘制验证码
    波形捕捉:(8)使用“捕捉缓冲区”
    DirectX编程:C#中利用Socket实现网络语音通信[初级版本]
    DirectX编程:[初级]C#中利用DirectSound播放WAV格式声音[最少只要4句话]
    GroupingView控件 使用经验
    Dot Net下实现屏幕图像差异获取v2.0
  • 原文地址:https://www.cnblogs.com/xuyy-isee/p/10552818.html
Copyright © 2011-2022 走看看