zoukankan      html  css  js  c++  java
  • (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;
        }
    };
  • 相关阅读:
    系统吞吐量、TPS(QPS)、用户并发量、性能测试概念和公式(分享二十二)
    某云数据中心网络解决方案(分享二十一)
    oracle 12c 管理(分享二十)
    Codeforces 356D 倍增优化背包
    Codeforces 360D Levko and Sets (数论好题)
    gym/102253C Colorful Tree 树上计数
    Codeforces 360E 贪心 最短路
    Codeforces 360C DP 计算贡献
    Codeforces 354B 博弈, DP,记忆化搜索
    Codeforces 354C 暴力 数论
  • 原文地址:https://www.cnblogs.com/Bella2017/p/11149677.html
Copyright © 2011-2022 走看看