• leetcode@ [79/140] Trie树应用 Word Search / Word Search II


    https://leetcode.com/problems/word-search/

    class Solution {
    public:
        struct Trie{
            Trie *next[57];
            bool isWord;
            Trie() {
                this->isWord = false;
                for(auto &c: next) c = NULL;
            }
        };
        void insert(Trie *root, string word) {
            Trie *p = root;
            for(auto &c: word) {
                int idx = c - 'A';
                if(!p->next[idx]) p->next[idx] = new Trie();
                p = p->next[idx];
            }
            p->isWord = true;
        }
        bool isPrefix(Trie *root, string word) {
            Trie *p = root;
            for(auto &c: word) {
                int idx = c - 'A';
                if(!p->next[idx]) return false;
                p = p->next[idx];
            }
            return true;        
        }
        bool isWord(Trie *root, string word) {
            Trie *p = root;
            for(auto &c: word) {
                int idx = c - 'A';
                if(!p->next[idx]) return false;
                p = p->next[idx];
            }
            return p->isWord;         
        }
        bool check(vector<vector<char>> &board, int x, int y) {
            int m = board.size(), n = board[0].size();
            if(x<0 || y<0 || x>=m || y>=n) return false;
            return true;
        }
        
        bool dfs(vector<vector<char>> &board, vector<vector<bool>> &vis, Trie *root, string s, int x, int y) {
            if(isWord(root, s)) 
                return true;
            }
            
            int dir[4][2] = {{1,0}, {0,1}, {-1,0}, {0,-1}};
            for(int i=0;i<4;++i) {
                int nx = x + dir[i][0], ny = y + dir[i][1];
                if(check(board, nx, ny) && !vis[nx][ny]) {
                    if(isPrefix(root, s + board[nx][ny])) {
                        vis[nx][ny] = true;
                        if(dfs(board, vis, root, s + board[nx][ny], nx, ny)) return true;
                        vis[nx][ny] = false;    
                    }
                }
            }
            return false;
        }
        bool exist(vector<vector<char>>& board, string word) {
            if (board.empty() || board[0].empty() || word.empty()) return false;
            
            string s = "";
            int m = board.size(), n = board[0].size();
            vector<vector<bool>> vis(m, vector<bool>(n, false));
            
            Trie *root = new Trie();
            insert(root, word);
            
            for(int i=0;i<m;++i) {
                for(int j=0;j<n;++j) {
                    //if(isWord(root, s + board[i][j])) return true;
                    if(isPrefix(root, s)) {
                        vis[i][j] = true;
                        if(dfs(board, vis, root, s + board[i][j], i, j)) return true;
                        vis[i][j] = false;    
                    }
                }
            }
            
            return false;
        }
    };
    View Code

    https://leetcode.com/problems/word-search-ii/

    struct TriNode {
        TriNode *ch[26];
        bool isWord;
        TriNode() : isWord(false) {
            for (auto &a : ch) a = NULL;
        }
    };
    class Solution {
    public:
        void insert(TriNode *root, string word) {
            TriNode *p = root;
            for (auto &a : word) {
                int i = a - 'a';
                if (p->ch[i] == NULL) p->ch[i] = new TriNode();
                p = p->ch[i];
            }
            p->isWord = true;
        }
        bool isPrefix(TriNode *root, string word) {
            TriNode *p = root;
            for (auto &a : word) {
                int i = a - 'a';
                if (p->ch[i] == NULL) return false;
                p = p->ch[i];
            }
            return true;
        }
        bool isWord(TriNode *root, string word) {
            TriNode *p = root;
            for (auto &a : word) {
                int i = a - 'a';
                if (p->ch[i] == NULL) return false;
                p = p->ch[i];
            }
            return p->isWord;
        }
        bool isValid(vector<vector<char>> &board, int x, int y) {
            int m = board.size(), n = board[0].size();
            if (x < 0 || x >= m || y < 0 || y >= n) return false;
            return true;
        }
        void dfs(vector<vector<char>> board, vector<vector<int>> visit, set<string> &st, string s, TriNode *root, int x, int y) {
            if(!isPrefix(root, s)) return;
            if(isWord(root, s)) {
                st.insert(s);
            }
            
            int dx[4] = {1, -1, 0, 0};
            int dy[4] = {0, 0, 1, -1};
            int xx, yy;
            for (int i = 0; i < 4; ++i) {
                xx = x + dx[i]; yy = y + dy[i];
                if (isValid(board, xx, yy) && !visit[xx][yy]) {
                    visit[xx][yy] = 1;
                    dfs(board, visit, st, s + board[xx][yy], root, xx, yy);
                    visit[xx][yy] = 0;
                }
            }
        }
        
        vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
            vector<string> res; res.clear();
            if (board.empty() || board[0].empty() || words.empty()) return res;
            
            TriNode *root = new TriNode();
            for(auto &word : words) insert(root, word);
            
            int m = board.size(), n = board[0].size();
            vector<vector<int>> visit(m, vector<int>(n, 0));
            
            string s="";
            set<string> st; st.clear();
            for (int i = 0; i < m; ++i) {
                for (int j = 0; j < n; ++j) {
                    visit[i][j] = 1;
                    dfs(board, visit, st, s + board[i][j], root, i, j);
                    visit[i][j] = 0;
                }
            }
    
            for (auto &word : st) res.push_back(word);
            return res;
        }
    };
    View Code
  • 相关阅读:
    计算机安装Fedora操作系统——Win10+Linux双系统
    性能测试——压力测试指标
    系统吞吐量(TPS)、用户并发量、性能测试概念和公式
    在 数学吧 看到一个 极限题
    东方学帝 和 K歌之王 的 科学观 和 科学方法 的 对比
    走一走 欧拉先生 走过 的 路
    推导一个 经典物理 里 的 黑洞 的 坍缩半径
    四色定理 太简单了 , 来 玩 n 维空间 里 的 x 色定理
    今天看到了一个 求 平面图形 Centroid 的 办法
    记录一下这几天的一些讨论
  • 原文地址:https://www.cnblogs.com/fu11211129/p/4952255.html
Copyright © 2020-2023  润新知