又称单词查找树,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;
}
};