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

 

posted @ 2015-11-10 11:00  流白  阅读(484)  评论(0编辑  收藏  举报