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

  1. class Solution {
  2. public:
  3. struct Trie{
  4. Trie *next[];
  5. bool isWord;
  6. Trie() {
  7. this->isWord = false;
  8. for(auto &c: next) c = NULL;
  9. }
  10. };
  11. void insert(Trie *root, string word) {
  12. Trie *p = root;
  13. for(auto &c: word) {
  14. int idx = c - 'A';
  15. if(!p->next[idx]) p->next[idx] = new Trie();
  16. p = p->next[idx];
  17. }
  18. p->isWord = true;
  19. }
  20. bool isPrefix(Trie *root, string word) {
  21. Trie *p = root;
  22. for(auto &c: word) {
  23. int idx = c - 'A';
  24. if(!p->next[idx]) return false;
  25. p = p->next[idx];
  26. }
  27. return true;
  28. }
  29. bool isWord(Trie *root, string word) {
  30. Trie *p = root;
  31. for(auto &c: word) {
  32. int idx = c - 'A';
  33. if(!p->next[idx]) return false;
  34. p = p->next[idx];
  35. }
  36. return p->isWord;
  37. }
  38. bool check(vector<vector<char>> &board, int x, int y) {
  39. int m = board.size(), n = board[].size();
  40. if(x< || y< || x>=m || y>=n) return false;
  41. return true;
  42. }
  43.  
  44. bool dfs(vector<vector<char>> &board, vector<vector<bool>> &vis, Trie *root, string s, int x, int y) {
  45. if(isWord(root, s))
  46. return true;
  47. }
  48.  
  49. int dir[][] = {{,}, {,}, {-,}, {,-}};
  50. for(int i=;i<;++i) {
  51. int nx = x + dir[i][], ny = y + dir[i][];
  52. if(check(board, nx, ny) && !vis[nx][ny]) {
  53. if(isPrefix(root, s + board[nx][ny])) {
  54. vis[nx][ny] = true;
  55. if(dfs(board, vis, root, s + board[nx][ny], nx, ny)) return true;
  56. vis[nx][ny] = false;
  57. }
  58. }
  59. }
  60. return false;
  61. }
  62. bool exist(vector<vector<char>>& board, string word) {
  63. if (board.empty() || board[].empty() || word.empty()) return false;
  64.  
  65. string s = "";
  66. int m = board.size(), n = board[].size();
  67. vector<vector<bool>> vis(m, vector<bool>(n, false));
  68.  
  69. Trie *root = new Trie();
  70. insert(root, word);
  71.  
  72. for(int i=;i<m;++i) {
  73. for(int j=;j<n;++j) {
  74. //if(isWord(root, s + board[i][j])) return true;
  75. if(isPrefix(root, s)) {
  76. vis[i][j] = true;
  77. if(dfs(board, vis, root, s + board[i][j], i, j)) return true;
  78. vis[i][j] = false;
  79. }
  80. }
  81. }
  82.  
  83. return false;
  84. }
  85. };

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

  1. struct TriNode {
  2. TriNode *ch[];
  3. bool isWord;
  4. TriNode() : isWord(false) {
  5. for (auto &a : ch) a = NULL;
  6. }
  7. };
  8. class Solution {
  9. public:
  10. void insert(TriNode *root, string word) {
  11. TriNode *p = root;
  12. for (auto &a : word) {
  13. int i = a - 'a';
  14. if (p->ch[i] == NULL) p->ch[i] = new TriNode();
  15. p = p->ch[i];
  16. }
  17. p->isWord = true;
  18. }
  19. bool isPrefix(TriNode *root, string word) {
  20. TriNode *p = root;
  21. for (auto &a : word) {
  22. int i = a - 'a';
  23. if (p->ch[i] == NULL) return false;
  24. p = p->ch[i];
  25. }
  26. return true;
  27. }
  28. bool isWord(TriNode *root, string word) {
  29. TriNode *p = root;
  30. for (auto &a : word) {
  31. int i = a - 'a';
  32. if (p->ch[i] == NULL) return false;
  33. p = p->ch[i];
  34. }
  35. return p->isWord;
  36. }
  37. bool isValid(vector<vector<char>> &board, int x, int y) {
  38. int m = board.size(), n = board[].size();
  39. if (x < || x >= m || y < || y >= n) return false;
  40. return true;
  41. }
  42. void dfs(vector<vector<char>> board, vector<vector<int>> visit, set<string> &st, string s, TriNode *root, int x, int y) {
  43. if(!isPrefix(root, s)) return;
  44. if(isWord(root, s)) {
  45. st.insert(s);
  46. }
  47.  
  48. int dx[] = {, -, , };
  49. int dy[] = {, , , -};
  50. int xx, yy;
  51. for (int i = ; i < ; ++i) {
  52. xx = x + dx[i]; yy = y + dy[i];
  53. if (isValid(board, xx, yy) && !visit[xx][yy]) {
  54. visit[xx][yy] = ;
  55. dfs(board, visit, st, s + board[xx][yy], root, xx, yy);
  56. visit[xx][yy] = ;
  57. }
  58. }
  59. }
  60.  
  61. vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
  62. vector<string> res; res.clear();
  63. if (board.empty() || board[].empty() || words.empty()) return res;
  64.  
  65. TriNode *root = new TriNode();
  66. for(auto &word : words) insert(root, word);
  67.  
  68. int m = board.size(), n = board[].size();
  69. vector<vector<int>> visit(m, vector<int>(n, ));
  70.  
  71. string s="";
  72. set<string> st; st.clear();
  73. for (int i = ; i < m; ++i) {
  74. for (int j = ; j < n; ++j) {
  75. visit[i][j] = ;
  76. dfs(board, visit, st, s + board[i][j], root, i, j);
  77. visit[i][j] = ;
  78. }
  79. }
  80.  
  81. for (auto &word : st) res.push_back(word);
  82. return res;
  83. }
  84. };

leetcode@ [79/140] Trie树应用 Word Search / Word Search II的更多相关文章

  1. 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design

    字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...

  2. leetcode 211. Add and Search Word - Data structure design Trie树

    题目链接 写一个数据结构, 支持两种操作. 加入一个字符串, 查找一个字符串是否存在.查找的时候, '.'可以代表任意一个字符. 显然是Trie树, 添加就是正常的添加, 查找的时候只要dfs查找就可 ...

  3. leetcode 79. Word Search 、212. Word Search II

    https://www.cnblogs.com/grandyang/p/4332313.html 在一个矩阵中能不能找到string的一条路径 这个题使用的是dfs.但这个题与number of is ...

  4. LeetCode 79 Word Search(单词查找)

    题目链接:https://leetcode.com/problems/word-search/#/description 给出一个二维字符表,并给出一个String类型的单词,查找该单词是否出现在该二 ...

  5. [leetcode trie]211. Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

  6. [LeetCode] 79. Word Search 单词搜索

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  7. [leetcode]79.Search Word 回溯法

    /** * Given a 2D board and a word, find if the word exists in the grid. The word can be constructed ...

  8. [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

  9. leetcode@ [211] Add and Search Word - Data structure design

    https://leetcode.com/problems/add-and-search-word-data-structure-design/ 本题是在Trie树进行dfs+backtracking ...

随机推荐

  1. uva 108

    降维  枚举行累加   然后求单行上最大连续和 #include <iostream> #include <cstring> #include <cstdio> # ...

  2. python调试总结

    调试通常采用两种方式,打印日志调试以及运行时实时跟踪调试. 一.打印日志: 1. print不要看不起print,这是一切调试的起点,即便是调试Java或者C这种巨麻烦的编译语言,print仍然是常用 ...

  3. 强大的CImage类

    这下有了CImage类,处理其他类型的图片不再寻找第三方类库了.加载到对话框背景的代码如下:  //从资源里载入背景JPEG图片 HRSRC hRsrc=::FindResource(AfxGetRe ...

  4. 【leetcode】Word Break (middle)

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  5. ORACLE 更新关联多张表

    UPDATE T_XMLC_BILL_HEAD_BAK T1 SET (T1.SENDRECEIVEFLAG, T1.SENDRECEIVEOPERATOR, T1.SENDRECEIVEDATE, ...

  6. spring事物的传播行为

    1.spring事物的传播行为,主要是用来解决业务层拥有事物的方法,相互调用的问题. 2.声明事物, 在代码执行前,开启事务.代码执行完,提交事务 3.spring并没有提供事务具体的处理,而只是调用 ...

  7. Servlet课程0425(六) 不经过验证直接跳转---session实现不同页面之间共享数据

    在地址栏直接输入http://localhost:8080/myWebSite/wel 会发现页面也能跳转,只不过用户名和密码都为空,这是不可以的,因为没有经过验证非法登录了 Welcome,hell ...

  8. 选择排序的openMP实现

    // test.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <stdio.h> #include < ...

  9. 解读Q_GLOBAL_STATIC(QFontDatabasePrivate, privateDb)

    根据 src/corelib/global.h template <typename T>class QGlobalStatic{public: T *pointer; inline QG ...

  10. 算法总结之欧拉函数&中国剩余定理

    算法总结之欧拉函数&中国剩余定理 1.欧拉函数 概念:在数论,对正整数n,欧拉函数是少于或等于n的数中与n互质的数的数目. 通式:φ(x)=x(1-1/p1)(1-1/p2)(1-1/p3)( ...