leetocde212. 单词搜索 II/前缀树

212. 单词搜索 II

给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词。

单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母在一个单词中不允许被重复使用。

示例:

输入: 
words = ["oath","pea","eat","rain"] and board =
[
  ['o','a','a','n'],
  ['e','t','a','e'],
  ['i','h','k','r'],
  ['i','f','l','v']
]

输出: ["eat","oath"]

说明:
你可以假设所有输入都由小写字母 a-z 组成。

提示:

  • 你需要优化回溯算法以通过更大数据量的测试。你能否早点停止回溯?
  • 如果当前单词不存在于所有单词的前缀中,则可以立即停止回溯。什么样的数据结构可以有效地执行这样的操作?散列表是否可行?为什么? 前缀树如何?如果你想学习如何实现一个基本的前缀树,请先查看这个问题: 实现Trie(前缀树)。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/word-search-ii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

前缀树

参考:https://blog.csdn.net/qq_41627847/article/details/106513167

  • 将单词放在前缀树中
  • 从board中的每一个位置开始,判断前缀树中的单词是否存在,注意在判断的过程中为了避免重复使用board中的字符,需要进行标记;为了避免重复搜索单词,对单词进行标记
//建立前缀树
class Tries{
    
    
public:
    bool isEnd;//标志当前字符串是否结束
    string str;//保存当前字符串
    Tries * next[26];

    Tries(){
    
    
        str = "";
        isEnd = false;
        memset(next, NULL, sizeof(next));
    }

    //插入
    void insert(string word){
    
    
        Tries * root = this;
        for(auto w : word){
    
    
            if(root->next[w - 'a'] == NULL)
                root->next[w - 'a'] = new Tries();
            root = root->next[w - 'a'];
        }
        root->isEnd = true;
        root->str = word;
        //cout << word << endl; 
    }

    //查找:搜索前缀树中的字符串在不在board中,
    //从board中的每一个字符开始深搜,在前缀树中搜到了该字符进行标记,并在搜索该单词的过程中进行标记board,以免重复利用board中的单词,将结果保存在res中
    void serch(vector<string>& res, vector<vector<char>>& board){
    
    
        Tries * root = this;
        for(int i = 0; i < board.size(); ++i){
    
    
            for(int j = 0; j < board[0].size(); ++j){
    
    
                dfs(res, board, root, i, j);
            }
        }
    }

    void dfs(vector<string>& res, vector<vector<char>>& board, Tries *node, int x, int y){
    
    
        if(node->isEnd == true){
    
    
            //cout << node->str << endl;
            res.push_back(node->str);
            node->isEnd = false;//标记该单词,以免重复判断
            return;
        }

        //坐标不合理,当前两个字符不相等,当前board中的字符已经搜索过
        if(x < 0 || x == board.size() || y < 0 || y == board[0].size() || board[x][y] == '#' ||node->next[board[x][y] - 'a'] == NULL)
            return;
        node = node->next[board[x][y] - 'a'];//node指向下一个节点
        //四个方向dfs
        char temp = board[x][y];
        board[x][y] = '#';
//搜索的顺序不对也没有办法通过?????
        dfs(res, board, node, x - 1, y);
        dfs(res, board, node, x + 1, y);
        dfs(res, board, node, x, y - 1);
        dfs(res, board, node, x, y + 1);

        board[x][y] = temp;//回溯
        
    }
};
class Solution {
    
    
public:
    vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
    
    
        Tries *T = new Tries();
        for(auto w : words){
    
    
            T->insert(w);
        }
        vector<string> res;
        T->serch(res, board);
        return res;
    }
};

猜你喜欢

转载自blog.csdn.net/qq_31672701/article/details/108659891