简单的字典树(前缀树)

写这个树,主要是为了完成这道题目。http://hihocoder.com/problemset/problem/1014

代码如下,注释有比较详细的解释

 1 #include <iostream>
 2 #include <string>
 3 #include <typeinfo>
 4 #include <vector>
 5 using namespace std;
 6
 7 /************************************************************************/
 8 /* 树节点是两个数组,一个存储字符对应的下一节点指针
 9 /* 另一个数组存储以该字符结尾的所有前缀次数*/
10 /************************************************************************/
11 class TreeNode
12 {
13 public:
14     TreeNode()
15     {
16         for (int i=0;i<26;i++)
17         {
18             nextArray[i]=NULL;
19             Count[i]=0;
20         }
21     }
22     TreeNode *nextArray[26];
23     int Count[26];
24 };
25
26 class TripTree
27 {
28 public:
29     TripTree()
30     {
31         head = NULL;
32     }
33     void insertStr(const string &str)
34     {
35         if(NULL == head)
36         {
37             head = new TreeNode;
38         }
39         int i=0;
40         TreeNode *temp = head;
41         const int len = str.length();
42         //从头到尾的构造字典树,len-1是为了防止越界
43         for(;i<len-1;i++)
44         {
45             temp->Count[str[i]-‘a‘]++;
46             if (temp->nextArray[str[i]-‘a‘] == NULL)
47             {
48                 temp->nextArray[str[i]-‘a‘] = new TreeNode;
49             }
50             temp = temp->nextArray[str[i]-‘a‘];
51
52         }
53         temp->Count[str[i]-‘a‘]++;//处理最后一个字符,因为是最后一个字符,就没有后继节点,此处不修改对应的下一个指针
54     }
55     int findPre(const string &str)
56     {
57         int i=0;
58         TreeNode *temp = head;
59         const int len = str.length();
60         for(;i<len-1;i++)
61         {
62             if(temp->Count[str[i]-‘a‘] == 0||temp->nextArray[str[i]-‘a‘] == NULL)
63                 return 0;
64             temp = temp->nextArray[str[i]-‘a‘];
65         }
66         return temp->Count[str[i]-‘a‘];
67     }
68     TreeNode *head;
69
70 };
71
72 int main()
73 {
74     string str;
75     int n;
76     TripTree obj;
77     while (cin >> n)
78     {
79         while(n--)
80         {
81             cin >> str;
82             obj.insertStr(str);
83
84         }
85         cin >> n;
86         while(n--)
87         {
88             cin >> str;
89             cout << obj.findPre(str)<<endl;
90
91         }
92     }
93     return 0;
94 }
时间: 2024-08-25 09:13:39

简单的字典树(前缀树)的相关文章

9-11-Trie树/字典树/前缀树-查找-第9章-《数据结构》课本源码-严蔚敏吴伟民版

课本源码部分 第9章  查找 - Trie树/字典树/前缀树(键树) ——<数据结构>-严蔚敏.吴伟民版        源码使用说明  链接??? <数据结构-C语言版>(严蔚敏,吴伟民版)课本源码+习题集解析使用说明        课本源码合辑  链接??? <数据结构>课本源码合辑        习题集全解析  链接??? <数据结构题集>习题解析合辑        本源码引入的文件  链接? Status.h.Scanf.c        相关测试数据

【数据结构】字典树/Trie树/前缀树 - 字符串的统计、排序和保存

字典树 描述 字典树,又称单词查找树.Trie树.前缀树,是一种树形结构,是一种哈希树的变种. 典型应用是用于统计.排序和保存大量的字符串(但不仅限于字符串). 常见操作有插入和查找,删除操作少见. 性质 根节点不包含字符 除根节点外每一个节点都只包含一个字符 从根节点到某一节点,路径上经过的字符连接起来,为该节点对应的字符串 每个节点的所有子节点包含的字符都不相同 优点 利用字符串的公共前缀来减少查询时间 最大限度地减少无谓的字符串比较 查询效率比哈希树高 自带字典序排序 直接判断重复,或者记

LeetCode OJ:Implement Trie (Prefix Tree)(实现一个字典树(前缀树))

Implement a trie with insert, search, and startsWith methods. 实现字典树,前面好像有道题做过类似的东西,代码如下: 1 class TrieNode { 2 public: 3 // Initialize your data structure here. 4 TrieNode():isLeaf(false) 5 { 6 for(auto & t : dic){ 7 t = NULL; 8 } 9 } 10 TrieNode * di

[LeetCode] 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. http://dongxicheng.org/structure/trietree/

【数据结构】前缀树/字典树/Trie

[前缀树] 用来保存一个映射(通常情况下 key 为字符串  value 为字符串所代表的信息) 例如:一个单词集合 words = {  apple, cat,  water  }   其中 key 为单词      value 代表该单词是否存在 words[ 'apple' ] = 存在     而     word[ ' abc' ] = 不存在 图示:一个保存了8个键的trie结构,"A", "to", "tea", "ted

Phone List(简单的字典树插入操作)

Phone List Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 11655    Accepted Submission(s): 3970 Problem Description Given a list of phone numbers, determine if it is consistent in the sense th

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

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

【学习总结】数据结构-Trie/前缀树/字典树-及其最常见的操作

Trie/前缀树/字典树 Trie (发音为 "try") 或前缀树是一种树数据结构,用于检索字符串数据集中的键. 一种树形结构,是一种哈希树的变种. 典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计. 优点:利用字符串的公共前缀来减少查询时间,最大限度地减少无谓的字符串比较,查询效率比哈希树高. 应用: 自动补全 END 原文地址:https://www.cnblogs.com/anliux/p/12590368.html

leetcode 208. 实现 Trie (前缀树)/字典树

实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作. 示例: Trie trie = new Trie(); trie.insert("apple");trie.search("apple"); // 返回 truetrie.search("app"); // 返回 falsetrie.startsWith("app"); // 返回 truetrie.insert(&q

字典树Trie树

一.字典树 字典树--Trie树,又称为前缀树(Prefix Tree).单词查找树或键树,是一种多叉树结构. 上图是一棵Trie树,表示了关键字集合{"a", "to", "tea", "ted", "ten", "i", "in", "inn"} .从上图可以归纳出Trie树的基本性质: 1. 根节点不包含字符,除根节点外的每一个子节点都包含一