字典树搜索LINTCODE 题目Add and Search Word

  1. public class WordDictionary {
  2. private TrieNode root = new TrieNode();
  3. public void addWord(String word) {
  4. Map<Character, TrieNode> children = root.children;
  5. for(int i=0; i<word.length(); i++) {
  6. char c = word.charAt(i);
  7. TrieNode t;
  8. if(children.containsKey(c)) {
  9. t = children.get(c);
  10. } else {
  11. t = new TrieNode(c);
  12. children.put(c, t);
  13. }
  14. children = t.children;
  15. if(i==word.length()-1) t.leaf=true;
  16. }
  17. }
  18. public boolean search(String word) {
  19. return searchNode(word, root);
  20. }
  21. public boolean searchNode(String word, TrieNode tn) {
  22. if(tn==null) return false;
  23. if(word.length() == 0 ) return tn.leaf;
  24. Map<Character, TrieNode> children = tn.children;
  25. TrieNode t = null;
  26. char c = word.charAt(0);
  27. if(c==‘.‘) {
  28. for(char key : children.keySet() ) {
  29. if(searchNode(word.substring(1), children.get(key) )) return true;
  30. }
  31. return false;
  32. } else if(!children.containsKey(c)) {
  33. return false;
  34. } else {
  35. t = children.get(c);
  36. return searchNode(word.substring(1), t);
  37. }
  38. }
  39. }
  40. class TrieNode {
  41. // Initialize your data structure here.
  42. char c;
  43. boolean leaf;
  44. HashMap<Character, TrieNode> children = new HashMap<Character, TrieNode>();
  45. public TrieNode(char c) {
  46. this.c = c;
  47. }
  48. public TrieNode(){};
  49. }
时间: 2024-10-12 20:36:55

字典树搜索LINTCODE 题目Add and Search Word的相关文章

[LintCode] Add and Search Word 添加和查找单词

Design a data structure that supports the following two operations: addWord(word) and search(word) search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter.Notic

[LeetCode][JavaScript]Add and Search Word - Data structure design

Add and Search Word - Data structure design Design a data structure that supports the following two operations: void addWord(word) bool search(word) search(word) can search a literal word or a regular expression string containing only letters a-z or 

【LeetCode】211. Add and Search Word - Data structure design

Add and Search Word - Data structure design Design a data structure that supports the following two operations: void addWord(word) bool search(word) search(word) can search a literal word or a regular expression string containing only letters a-z or 

211 Add and Search Word - Data structure design--- back tracking, map, set 待续 trie

题意: 设计个字典查询系统, 有 add 和search 两种操作, add 是加入单词到字典里, search 时 可以用 点号通配符 ".", 点号可以匹配一个字母. 分析: 当search 时为 通配符时, 如果直接用back tracking产生 a-z, 比如 有7个点号, 就得生成  26^7 个组合,会TLE. 以下是TLE的code: class WordDictionary { /** Initialize your data structure here. */ S

(Data structure)Implement Trie And Add and Search Word

Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs are consist of lowercase letters a-z solution: class TrieNode { // Initialize your data structure here. boolean isEnd; Trie

LeetCode 211. Add and Search Word - Data structure design(字典树)

题目 字典树. class WordDictionary { public: int map[100005][26]; int tag[100005]; int num; /** Initialize your data structure here. */ WordDictionary() { memset(map,0,sizeof(map)); memset(tag,0,sizeof(tag)); num=0; } /** Adds a word into the data structur

POJ 1204 Word Puzzles(字典树+搜索)

题意:在一个字符矩阵中找每个给定字符串的匹配起始位置和匹配方向(A到H表示八个方向): 思路:将给定字符串插入字典树中,遍历字符矩阵,在每个字符处向八个方向用字典树找. #include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef struct node { int num; node *next[26]; }node; node *head; char str[1

LeetCode OJ:Add and Search Word - Data structure design(增加以及搜索单词)

Design a data structure that supports the following two operations: void addWord(word) bool search(word) search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter

HDU 1298 T9(字典树+搜索)

题意:每组有n个字符串,每个串对应一个权值,给出一个手机按键表,每个数字键可对应按出几个字母,m个询问,给出一串数字(最后一位不计),求该数字串对应的权值最大的字符串(将数字串每个前缀对应的字符串输出): 思路:将n个字符串插入字典树,在串的查询操作上进行深搜以便更新最大值,并且每个数字对应几个字符,分别遍历一下.经典题. #include<cstdio> #include<cstring> #include<algorithm> using namespace std