• 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
  • 相关阅读:
    MainFrm.cpp
    MyView.h
    我的东软实习项目一:车牌识别之MFC----MyView.cpp
    洛谷P1055 字符串的处理-----ISBN
    跳出多重循环------设置标志量
    单链表的创建及操作
    线性表的建立及运算
    JVM--你常见的jvm 异常有哪些? 代码演示:StackOverflowError , utOfMemoryError: Java heap space , OutOfMemoryError: GC overhead limit exceeded, Direct buffer memory, Unable_to_create_new_native_Thread, Metaspace
    JVM-gcRoots 和 强引用,软引用, 弱引用, 虚引用, 代码演示和应用场景
    JUC 并发编程--08,线程池,三大方法,七大参数,4种拒绝策略,代码演示
  • 原文地址:https://www.cnblogs.com/fu11211129/p/4952255.html
Copyright © 2020-2023  润新知