这一篇刷的主要是前缀树,LC208、LC211、LC421较好的理解Trie Tree。
较清晰的blog
前缀树、字典树、Trie树,是一种多叉树,一般是26个叉的,节点类型是一个isend判断是否是单词结尾,在叶节点为true,其他节点为false。还有个子节点数组。
主要操作有三个:建树(插入)、查询、前缀。
建树(插入)
就是给一个word,建立一条路径。从根节点的子节点开始和word字符串的每一个字符匹配,如果匹配上了就变到下一级,没匹配上就在当前char的next数组中建立一个节点,继续下一级。
查询
和建树差不多,就是和word每一个字符匹配next数组,匹配上了继续下一层,没匹配上就return false,如果最后一层了,判断树上的节点结束了没,return isend即可。
前缀
前缀和查询一样,不过最后不用检查结束了没,直接返回true即可。
1 LC208 模板
class Trie {
private:
bool isend;
Trie * next[26];
public:
/** Initialize your data structure here. */
Trie() {
isend = false;
memset(next, 0, sizeof(next));
}
/** Inserts a word into the trie. */
void insert(string word) {
Trie* node = this;
for(char ch: word)
{
if(node->next[ch - 'a'] == NULL)
node->next[ch - 'a'] = new Trie();
node = node->next[ch - 'a'];
}
node->isend = true;
}
/** Returns if the word is in the trie. */
bool search(string word) {
Trie* node = this;
for(char ch : word)
{
if(node->next[ch - 'a'] != NULL)
node = node->next[ch - 'a'];
else return false;
}
return node->isend;
}
/** Returns if there is any word in the trie that starts with the given prefix. */
bool startsWith(string prefix) {
Trie* node = this;
for(char ch : prefix)
{
if(node->next[ch - 'a'] != NULL)
node = node->next[ch - 'a'];
else return false;
}
return true;
}
};
/**
* Your Trie object will be instantiated and called as such:
* Trie* obj = new Trie();
* obj->insert(word);
* bool param_2 = obj->search(word);
* bool param_3 = obj->startsWith(prefix);
*/
2 LC211前缀树 + dfs
这道题主要是在.的处理,需要dfs回溯一下,还是掌握的不是很好,不能很快的写出来,继续刷题吧!
class WordDictionary {
private:
bool isend;
WordDictionary* next[27];
public:
/** Initialize your data structure here. */
WordDictionary() {
isend = false;
memset(next, 0, sizeof(next));
}
void addWord(string word) {
WordDictionary* node = this;
for(char ch : word)
{
if(ch != '.' && node->next[ch - 'a'] == NULL)
node->next[ch - 'a'] = new WordDictionary();
node = node->next[ch - 'a'];
}
node->isend = true;
}
bool dfs(WordDictionary* node, string& str, int idx)
{
if(idx == str.size())
return node->isend;
if(str[idx] != '.')
{
if(node->next[str[idx] - 'a'] != NULL)
return dfs(node->next[str[idx] - 'a'], str, idx + 1);
return false;
}
for(int i = 0; i < 26; i++)
{
if(node->next[i] != NULL && dfs(node->next[i], str, idx + 1))
return true;
}
return false;
}
bool search(string word) {
WordDictionary* node = this;
return dfs(node, word, 0);
}
};
/**
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary* obj = new WordDictionary();
* obj->addWord(word);
* bool param_2 = obj->search(word);
*/
3 LC421前缀树应用
嘿嘿,还是我的生日呢
将数字转化成二进制,都统一成32位,前面补0,从高位开始存储到树中。
对每个数字找最大异或,异或在不同时为1,也就是尽可能从高位开始不同,实在没有也没有办法。
void tran(int val, vector<int>& v)
{
int i = 0;
while(val)
{
v[i++] = val % 2;
val = val >> 1;
}
reverse(v.begin(), v.end());
}
class Trie
{
private:
Trie* next[2];
public:
Trie();
int find(int val);
void insert(int val);
};
Trie::Trie()
{
memset(next, 0, sizeof(next));
}
void Trie::insert(int val)
{
Trie* node = this;
vector<int> bit(32, 0);
tran(val, bit);
for(int i = 0; i < 32; i++)
{
if(node->next[bit[i]] == NULL)
node->next[bit[i]] = new Trie();
node = node->next[bit[i]];
}
}
int Trie::find(int val)
{
Trie* node = this;
vector<int> bit(32, 0);
tran(val, bit);
int ans = 0;
for(int i = 0; i < 32; i++)
{
int p = bit[i];
if(node->next[1 - p] != 0)
{
ans = ans * 2 + 1;
node = node->next[1 - p];
}
else if(node->next[p] != 0)
{
ans = ans * 2;
node = node->next[p];
}
}
return ans;
}
class Solution {
public:
int findMaximumXOR(vector<int>& nums) {
Trie * root = new Trie();
for(int i = 0; i < nums.size(); i++)
root->insert(nums[i]);
int ma = 0;
for(int i = 0; i < nums.size(); i++)
if(ma < root->find(nums[i]))
ma = root->find(nums[i]);
return ma;
}
};