trie tree(字典树)

hihocoder题目(http://hihocoder.com/problemset):#1014  trie树

  1 #include <iostream>
  2 using namespace std;
  3 class trieTree
  4 {
  5 public:
  6     trieTree()
  7     {
  8         isword=false;
  9         for (int i=0;i<26;i++)
 10         {
 11             next[i]=NULL;
 12         }
 13     }
 14     ~trieTree()
 15     {
 16         for (int i=0;i<26;i++)
 17         {
 18             if (next[i]!=NULL)
 19             {
 20                 destory(next[i]);
 21             }
 22         }
 23     }
 24     void destory(trieTree* index);
 25     int insertWord(char*);
 26     int searchWord(char*);
 27     int findWord(trieTree* index);
 28 private:
 29     trieTree* next[26];
 30     bool isword;
 31 };
 32 int main()
 33 {
 34     int dictionarySize;
 35     trieTree root;
 36     char word[10];
 37     cin>>dictionarySize;
 38     int i=0;
 39     for(i=0;i<dictionarySize;i++)
 40     {
 41         cin>>word;
 42         root.insertWord(word);
 43     }
 44     cin>>dictionarySize;
 45     for(i=0;i<dictionarySize;i++)
 46     {
 47         cin>>word;
 48         cout<<root.searchWord(word)<<endl;
 49     }
 50
 51     return 0;
 52 }
 53 int trieTree::insertWord(char* word)
 54 {
 55     trieTree* index=this;
 56     while(*word)
 57     {
 58         if(!index->next[*word-‘a‘])
 59         {
 60             index->next[*word-‘a‘]=new trieTree;
 61         }
 62         if (!index->next[*word-‘a‘])
 63         {
 64             return -1;
 65         }
 66         else
 67         {
 68             index=index->next[*word-‘a‘];
 69         }
 70         word++;
 71     }
 72     index->isword=true;
 73     return 0;
 74 }
 75 int trieTree::searchWord(char* word)
 76 {
 77     trieTree* index=this;
 78     int count=0;
 79     while(*word)
 80     {
 81         if(index->next[*word-‘a‘]==NULL)
 82         {
 83             return 0;
 84         }
 85         index=index->next[*word-‘a‘];
 86         word++;
 87     }
 88     count=findWord(index);
 89     return count;
 90 }
 91 int trieTree::findWord(trieTree* index)
 92 {
 93     int count=0;
 94     if(index->isword)
 95         count++;
 96     for(int i=0;i<26;i++)
 97     {
 98         if(index->next[i]!=NULL)
 99         {
100             count+=findWord(index->next[i]);
101         }
102     }
103     return count;
104 }
105 void trieTree::destory(trieTree* index)
106 {
107     for (int i=0;i<26;i++)
108     {
109         if (index->next[i]!=NULL)
110         {
111             destory(index->next[i]);
112             index->next[i]=NULL;
113         }
114     }
115     delete index;
116 }

通过编译之后,发现提交上去还是错了,Wrong Answer!

时间: 2024-12-23 14:08:18

trie tree(字典树)的相关文章

uva 1556 - Disk Tree(字典树)

题目连接:uva 1556 - Disk Tree 题目大意:给出N个目录关系,然后按照字典序输出整个文件目录. 解题思路:以每个目录名作为字符建立一个字典树即可,每个节点的关系可以用map优化. #include <cstdio> #include <cstring> #include <map> #include <string> #include <iostream> #include <algorithm> using nam

常用算法之Trie【字典树,前缀树】

Trie中文名又叫做字典树,前缀树等,因为其结构独有的特点,经常被用来统计,排序,和保存大量的字符串,经常见于搜索提示,输入法文字关联等,当输入一个值,可以自动搜索出可能的选择.当没有完全匹配的结果时,可以返回前缀最为相似的可能. 其实腾讯的面试题有一个:如何匹配出拼写单词的正确拼写.其实用匹配树非常合适. 基本性质: 1.根节点不含有字符,其余各节点有且只有一个字符. 2.根节点到某一节点中经过的节点存储的值连接起来就是对应的字符串. 3.每一个节点所有的子节点的值都不应该相同. 借用一下维基

trie(字典树)原理及C++代码实现

字典树,又称前缀树,是用于存储大量字符串或类似数据的数据结构. 它的原理是利用相同前缀来减少查询字符串的时间. 不同于BST把关键字保存在本结点中,TRIE可以想象成把关键字和下一个结点的指针绑定,事实上我也是用map来实现的,所以不熟悉map的提前熟悉map的操作. Tire的逻辑比较抽象,所以如果是第一次见到这种组织方式的建议先熟悉熟悉这种逻辑再开始写代码,这样会比较顺畅. 代码如下:(仅供参考) 1 struct Node { 2 public : 3 bool isWord; 4 uno

poj 2513 并查集,Trie(字典树), 欧拉路径

- Colored Sticks POJ - 2513 You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some color. Is it possible to align the sticks in a straight line such that the colors of the endpoints that touch are of the same color?

208. 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. 在Trie树中主要有3个操作,插入.查找和删除.一般情况下Trie树中很少存在删除单独某个结点的情况,因此只考虑删除整棵树. 1.插入 假设存在字符串str,Trie树的根结点为root.i=0,p=root. 1)取str[

trie(字典树) C++版本 Python版本

AcWing  835. Trie字符串统计    https://www.acwing.com/problem/content/837/ 维护一个字符串集合,支持两种操作: “I x”向集合中插入一个字符串x: “Q x”询问一个字符串在集合中出现了多少次. 共有N个操作,输入的字符串总长度不超过 105105,字符串仅包含小写英文字母. 输入格式 第一行包含整数N,表示操作数. 接下来N行,每行包含一个操作指令,指令为”I x”或”Q x”中的一种. 输出格式 对于每个询问指令”Q x”,都

ACM学习历程—HDU 5536 Chip Factory(xor &amp;&amp; 字典树)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题目大意是给了一个序列,求(si+sj)^sk的最大值. 首先n有1000,暴力理论上是不行的. 此外题目中说大数据只有10组,小数据最多n只有100.(那么c*n^2的复杂度应该差不多) 于是可以考虑枚举i和j,然后匹配k. 于是可以先把所有s[k]全部存进一个字典树, 然后枚举s[i]和s[j],由于i.j.k互不相等,于是先从字典树里面删掉s[i]和s[j],然后对s[i]+s[j]这个

ACM学习历程—POJ 3764 The xor-longest Path(xor &amp;&amp; 字典树 &amp;&amp; 贪心)

题目链接:http://poj.org/problem?id=3764 题目大意是在树上求一条路径,使得xor和最大. 由于是在树上,所以两个结点之间应有唯一路径. 而xor(u, v) = xor(0, u)^xor(0, v). 所以如果预处理出0结点到所有结点的xor路径和,问题就转换成了求n个数中取出两个数,使得xor最大. 这个之前用字典树处理过类似问题. 代码: #include <iostream> #include <cstdio> #include <cst

字典树Trie

字典树Trie Trie,又称字典树,前缀树(prefix tree),是一种树形结构,用于保存大量的字符串. 它的优点是:利用字符串的公共前缀来节约存储空间.查找.插入复杂度为O(n),n为字符串长度. 它有3个基本性质: 1. 根节点不包含字符,除根节点外每一个节点都只包含一个字符. 2. 从根节点到某一节点,路径上经过的字符连接起来,为该节点对应的字符串. 3. 每个节点的所有子节点包含的字符都不相同. 假设有abc,abcd,abd, b, bcd,efg,hii这7个单词,可构建字典树

字典树 trie

字典树数据结构实现 1 public class Trie { 2 //字典树子节点最多值,任意一个单词都是由,26个字母组成的 3 private int SIZE = 26; 4 //根节点 5 private TrieNode root; 6 //初始化字典树 7 public Trie() { 8 root = new TrieNode(); 9 } 10 11 //字典树节点 12 private class TrieNode{ 13 //存放子节点的数组 14 private Tri