• (Trie树/暴力加剪枝)leetcode 720


    思路:Brute force + pruning

    用不用set来存储输入的words都可以。

    class Solution {
    public:
        string longestWord(vector<string>& words) {
            string best;  //存储当前最优解
            //unordered_set<string> dict(words.begin(), words.end());
            for(const string& word: words){
                //pruning
                if(word.length()<best.length() || (word.length()==best.length()&& word>best))
                    continue;
                string prefix;
                bool valid = true;
                for(int i=0; i<word.length()-1 && valid; ++i){
                    prefix += word[i];
                    if(find(words.begin(), words.end(), prefix)==words.end())
                    //if(!dict.count(prefix))
                        valid = false;
                }
                if(valid) best = word;
            }
            return best;
        }
    };

    2)找不到某个前缀 ,加上break 改进一下。

    class Solution {
    public:
        string longestWord(vector<string>& words) {
            string best;  //存储当前最优解
            //unordered_set<string> dict(words.begin(), words.end());
            for(const string& word: words){
                //pruning
                if(word.length()<best.length() || (word.length()==best.length()&& word>best))
                    continue;
                string prefix;
                bool valid = true;
                
                for(int i=0; i<word.length()-1 && valid; ++i){
                    prefix += word[i];
                    if(find(words.begin(), words.end(), prefix)==words.end()){
                        //当前单词的前缀在words中找不到
                        valid = false;
                        break;
                    }
                }
                
                if(valid) best = word;
            }
            return best;
        }
    };

    解法二:Trie树 加 剪枝:

    class Solution {
    public:
        struct TrieNode{
            TrieNode* child[26];
            bool is_word;
            TrieNode(): is_word(false){
                for(auto& a:child)
                    a = nullptr;
            }
            ~TrieNode(){
                delete[] child;
            }
        };
        struct Trie{
            TrieNode* root = new TrieNode();
            void insert(string word){
                TrieNode* p = root;
                for(char a: word){
                    int i = a-'a';
                    if(!p->child[i])
                        p->child[i] = new TrieNode();
                    p = p->child[i];
                }
                p->is_word = true;
            }
            
            bool hasAllpre(string& word){
                TrieNode* p = root;
                for(char c: word){
                    if(!p->child[c-'a'])
                        return false;
                    p = p->child[c-'a'];
                    if(!p->is_word)
                        return false;
                }
                return true;  
            }
        };
    
        string longestWord(vector<string>& words) {
            Trie trie;
            for(const string& word: words){
                trie.insert(word);
            }
            string best;
            for(string& word: words){
                if(word.length()<best.length() || (word.length()==best.length() && word>best))
                    continue;
                if(trie.hasAllpre(word))
                    best = word;
            }
            return best;
        }
    };
  • 相关阅读:
    Python基础-数据写入execl
    Python基础-读取excel
    table合并单元格colspan和rowspan
    从PHP客户端看MongoDB通信协议(转)
    win7环境下mongodb分片和移除
    32位下操作mongodb心得
    Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 1099 bytes) in
    js关闭当前页面(窗口)的几种方式
    js页面跳转 和 js打开新窗口 方法
    MongoDB中的_id和ObjectId
  • 原文地址:https://www.cnblogs.com/Bella2017/p/11149677.html
Copyright © 2020-2023  润新知