zoukankan      html  css  js  c++  java
  • Trie for string LeetCode

    Trie build and search 

     1 class TrieNode
     2 {
     3 public:
     4     TrieNode * next[26];
     5     bool is_word;
     6     TrieNode(bool b = false)
     7     {
     8         memset(next,0,sizeof(next));
     9         is_word = b;
    10     }
    11 };
    12 class Trie {
    13     TrieNode* root;
    14 public:
    15     /** Initialize your data structure here. */
    16     Trie() {
    17         root = new TrieNode();
    18     }
    19     
    20     /** Inserts a word into the trie. */
    21     void insert(string word) {
    22         TrieNode* p = root;
    23         for(int i=0;i<word.size();i++)
    24         {
    25             if(p->next[word[i]-'a']==NULL)
    26                 p->next[word[i]-'a']=new TrieNode();
    27             p = p->next[word[i]-'a'];
    28         }
    29         p->is_word=true;
    30     }
    31     
    32     /** Returns if the word is in the trie. */
    33     bool search(string word) {
    34         TrieNode* p = find(word);
    35         return p&&p->is_word;
    36     }
    37     
    38     /** Returns if there is any word in the trie that starts with the given prefix. */
    39     bool startsWith(string prefix) {
    40         return find(prefix);
    41     }
    42     
    43     TrieNode* find(string word)
    44     {
    45         TrieNode* p = root;
    46         for(int i=0;i<word.size();i++)
    47             if(p->next[word[i]-'a']==NULL)return NULL;
    48             else p=p->next[word[i]-'a'];
    49         return p;
    50     }
    51 };
    52 
    53 /**
    54  * Your Trie object will be instantiated and called as such:
    55  * Trie obj = new Trie();
    56  * obj.insert(word);
    57  * bool param_2 = obj.search(word);
    58  * bool param_3 = obj.startsWith(prefix);
    59  */

    加 . 正则匹配一个任意字母 ,递归处理,注意p应指向当前遍历字母对应的结点

     1 class TrieNode
     2 {
     3 public:
     4     TrieNode *next[26];
     5     bool is_word;
     6 
     7     TrieNode(bool b = false)
     8     {
     9         memset(next, 0, sizeof(next));
    10         is_word = b;
    11     }
    12 };
    13 
    14 class WordDictionary {
    15 public:
    16     /** Initialize your data structure here. */
    17     WordDictionary() {
    18         root = new TrieNode();
    19     }
    20 
    21     /** Adds a word into the data structure. */
    22     void addWord(string word) {
    23         TrieNode *p = root;
    24         for (int i = 0; i < word.size(); i++)
    25         {
    26             if (p->next[word[i] - 'a'] == NULL)
    27                 p->next[word[i] - 'a'] = new TrieNode();
    28             p = p->next[word[i] - 'a'];
    29         }
    30         p->is_word = true;
    31     }
    32 
    33     /** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
    34     bool search(string word) {
    35         return query(word.c_str(), root);
    36     }
    37 
    38 private:
    39     TrieNode* root;
    40     bool query(const char* word, TrieNode* node) 
    41     {
    42         TrieNode* p = node;
    43         for (int i = 0; word[i]; i++) 
    44         {
    45             if (p && word[i] != '.')
    46                 p = p ->next[word[i] - 'a'];
    47             else if (p && word[i] == '.')
    48             { 
    49                 TrieNode* tmp=p;
    50                 for (int j = 0; j < 26; j++)
    51                 {
    52                     p = tmp -> next[j];
    53                     if (query(word + i + 1, p))
    54                         return true;
    55                 }
    56             }
    57             else break;
    58         }
    59         return p && p -> is_word; 
    60     }
    61 };
    62 /**
    63  * Your WordDictionary object will be instantiated and called as such:
    64  * WordDictionary obj = new WordDictionary();
    65  * obj.addWord(word);
    66  * bool param_2 = obj.search(word);
    67  */

    word search II

    1. word list insert to Trie

    2. dfs search

     1 class TrieNode
     2 {
     3 public:
     4     TrieNode *next[26];
     5     string word;
     6 
     7     TrieNode()
     8     {
     9         memset(next, 0, sizeof(next));
    10         word = "";
    11     }
    12 };
    13 
    14 class Trie
    15 {
    16     
    17 public:
    18     TrieNode* root;
    19     Trie()
    20     {
    21         root = new TrieNode();
    22     }
    23 
    24     void insert(string s)
    25     {
    26         TrieNode *p = root;
    27         for (int i = 0; i < s.size(); i++)
    28         {
    29             if (p->next[s[i] - 'a'] == NULL)
    30                 p->next[s[i] - 'a'] = new TrieNode();
    31             p = p->next[s[i] - 'a'];
    32         }
    33         p->word = s;
    34     }
    35 
    36 };
    37 
    38 void dfs(int i, int j, TrieNode* p, vector<vector<char>>& board, vector<string> &res)
    39 {
    40     char c = board[i][j];
    41     if (c == '#' || p->next[c - 'a'] == NULL)return;
    42     p = p->next[c - 'a'];
    43     if (p->word != "")
    44     {
    45         res.push_back(p->word);//找到
    46         p->word = "";//去重
    47     }
    48     board[i][j] = '#';
    49     if (i > 0)dfs(i - 1, j, p, board, res);
    50     if (j > 0)dfs(i, j - 1, p, board, res);
    51     if (i < board.size() - 1)dfs(i + 1, j, p, board, res);
    52     if (j < board[0].size() - 1)dfs(i, j + 1, p, board, res);
    53     board[i][j] = c;
    54 }
    55 
    56 class Solution {
    57 public:
    58     vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
    59         Trie t;
    60         vector<string> res;
    61         for (int i = 0; i < words.size(); i++)
    62             t.insert(words[i]);
    63         for (int i = 0; i < board.size(); i++)
    64         for (int j = 0; j < board[0].size(); j++)
    65             dfs(i, j, t.root, board, res);
    66         return res;
    67     }
    68 };

     421 数组中任意两个数的最大异或值

    思路:建树插入所有数,对每个数按位查找异或值

     1 class Trie{
     2 public:
     3     Trie* children[2];
     4     Trie(){
     5         children[0]=NULL;
     6         children[1]=NULL;
     7     }
     8 };
     9 
    10 class Solution {
    11 public:
    12 int findMaximumXOR(vector<int>& nums) {
    13     if(nums.size()==0)return 0;
    14     //Init Trie
    15     Trie* root = new Trie();
    16     for(int num:nums)
    17     {
    18         Trie* curNode = root;
    19         for(int i=31;i>=0;i--)
    20         {
    21             int curBit = (num>>i)&1;
    22             if(curNode->children[curBit]==NULL)
    23             {
    24                 curNode->children[curBit] = new Trie();
    25             }
    26             curNode = curNode->children[curBit];
    27         }
    28     }
    29     int _max = 0xffffffff;
    30     for(int num:nums)
    31     {
    32         Trie* curNode = root;
    33         int curSum = 0;
    34         for(int i=31;i>=0;i--)
    35         {
    36             int curBit = (num>>i)&1;
    37             if(curNode->children[curBit^1]!=NULL)
    38             {
    39                 curSum += 1<<i;
    40                 curNode = curNode->children[curBit^1];
    41             }
    42             else
    43             {
    44                 curNode = curNode->children[curBit];
    45             }
    46         }
    47         _max = max(curSum,_max);
    48     }
    49     return _max;
    50 }
    51     
    52 
    53 };
  • 相关阅读:
    PHP学习当中遗漏的知识点
    sql必知必会(第四版) 学习笔记
    servlet 笔记
    sql server 快捷键
    233
    第 四 课 数据类型
    第三课 go语言基础语法
    第二课 go语言的结构
    第 1 课 Go 简介
    linux 学习 查看文件占用空间大小
  • 原文地址:https://www.cnblogs.com/demian/p/10538722.html
Copyright © 2011-2022 走看看