前缀树

11 篇文章 0 订阅
10 篇文章 0 订阅

又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种。典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计。它的优点是:利用字符串的公共前缀来减少查询时间,最大限度地减少无谓的字符串比较,查询效率比哈希树高。

代码实现如下:
主要的成员函数有两个:
search()判断某个字符串是否存在;
insert()插入一个字符串;

class node
{
    public:
    node(){
       for(int i=0;i<26;++i)
       {
           child[i]=0;
       }
    }

     node* child[26];
     bool isWord=false;
};



class Trie {
public:
    /** Initialize your data structure here. */
    Trie() {
        root=new node();   
    }
    
    /** Inserts a word into the trie. */
    void insert(string word) {
        auto x=root;
        for(auto c:word)
        {
            auto ret=c-'a';
            if((x->child)[ret]==nullptr)
            {
                (x->child)[ret]=new node();
            }   
             x=(x->child)[ret];
        }
        x->isWord=true;
    }
    
    /** Returns if the word is in the trie. */
    bool search(string word) {
        auto x=root;
        for(auto c:word)
        {
            auto ret=c-'a';
            if((x->child)[ret]==nullptr)return false;
            x=(x->child)[ret];   
        }
        if(x->isWord)return true;
        return false;
    }
    
    /** Returns if there is any word in the trie that starts with the given prefix. */
    bool startsWith(string prefix) {
        auto x=root;
        for(auto c:prefix)
        {
            auto ret=c-'a';
            if((x->child)[ret]==nullptr)return false;
            x=(x->child)[ret];   
        } 
        return true;
    }
    private:
    node *root;
   
};

应用前缀树可以解决一类字符串问题
leetcode 820
在这里插入图片描述

class Solution {   
public:
    int minimumLengthEncoding(vector<string>& words) {
     Trie trie;
     int res=0;
    sort(words.begin(),words.end(),[](string s1,string s2){return s1.size()>s2.size();});
     for(int i=0;i<words.size();++i)
     {
         if(!trie.search(words[i]))
         res+=words[i].size()+1;
         for(int k=0;k<words[i].size();++k){
             trie.insert(words[i].substr(k));     
         } 
     }
        return res;
    }
};

这道题也可以不用前缀树做;

class Solution {
public:
    int minimumLengthEncoding(vector<string>& words) {
        unordered_set<string> good(words.begin(), words.end());
        for (const string& word: words) {
            for (int k = 1; k < word.size(); ++k) {
                good.erase(word.substr(k));
            }
        }

        int ans = 0;
        for (const string& word: good) {
            ans += word.size() + 1;
        }
        return ans;
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值