(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;
    }
};
原文地址:https://www.cnblogs.com/Bella2017/p/11149677.html