TrieNode 字典树

这个题目可以很好地看做字典树在字符串搜索中的运用,在查找的过程中,为了匹配‘.’能够代替任一字符,结合使用了BFS

struct TrieNode2
{
    TrieNode2* next[26];
    bool isFinished; //用于标记,string的结束
    TrieNode2()
    {
        isFinished = false;
        for (int i=0;i<26;++i)
        {
            next[i] = NULL;
        }
    }
};

class WordDictionary {
public:
    // Adds a word into the data structure.
    void addWord(string word) {
        TrieNode2* p = root;
        for (char c:word)
        {
            if (p->next[c - 'a'] == NULL)p->next[c - 'a'] = new TrieNode2();
            p = p->next[c - 'a'];
        }
        p->isFinished = true;
    }

    // Returns if the word is in the data structure. A word could
    // contain the dot character '.' to represent any one letter.
    bool search(string word) {
        int n = word.size();
        if (n == 0)return true;
        queue<TrieNode2*>myqueue;  //bfs
        myqueue.push(root);
        TrieNode2* p;
        for (int i = 0;i < n;++i)
        {
            for (int j=0,s=myqueue.size();j<s;++j)
            {
                p = myqueue.front();
                myqueue.pop();
                if (word[i] == '.')
                {
                    for (int j = 0;j < 26;j++)
                    {
                        if (p->next[j])myqueue.push(p->next[j]);
                    }
                }
                else
                {
                    if (p->next[word[i] - 'a'])
                    myqueue.push(p->next[word[i] - 'a']);
                }
            }       
            if (myqueue.empty())return false;//如果断层,则可以直接return false
        }
        while (!myqueue.empty())
        {
            p = myqueue.front();
            myqueue.pop();
            if (p->isFinished)return true;
        }
        return false;
    }
    WordDictionary()  //初始构造函数
    {
        root = new TrieNode2();
    }
private:
    TrieNode2* root;
};

// Your WordDictionary object will be instantiated and called as such:
// WordDictionary wordDictionary;
// wordDictionary.addWord("word");
// wordDictionary.search("pattern");

一般情况下,如果只是求一bool的结果,DFS更优,此题的DFS版本为

bool search_DFS(string word)
    {
        if (word.size() == 0)return true;
        return __search_DFS(word, 0, root);
    }
    bool __search_DFS(string word, int pos, TrieNode2* root)
    {
        if (root == NULL)return false;
        if (pos >= word.size())return root->isFinished;
        if (word[pos] == '.')
        {
            for (int i = 0;i < 26;i++)
            {
                if (__search_DFS(word, pos + 1, root->next[i]))return true;
            }
            return false;
        }
        else
            return __search_DFS(word, pos + 1, root->next[word[pos] - 'a']);
    }

TrieNode结构体

  1. root不存储信息,作为一个起始节点
  2. next数组变形,在全部小写的子母中,用26个
  3. 每个节点的内容,可以包含信息,此题包含的是这个string是否到此结束
点赞