字典树 trie树 学习
一字典树
从根节点到某一节点,路径上经过的字符串连接起来,为该节点对应的字符串;
每个节点的所有子节点包含的字符都不相同。


/*
字典树学习: 输入n个单词 举出一个单词出现的次数。 10 apple
ppppp
hello
hello
need
bee
bee
none
you
apple need
==1
==Program ended with exit code: 0 */
//
// main.cpp
// CPlusDemo
//
// Created by HF on 2018/5/15.
// Copyright © 2018年 HF-Liqun. All rights reserved.
// #include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
using namespace std; #define MAX 26 //the total number of alphabet is 26, a...z struct DicTrie{
bool isTerminal;//是否是单词结束标志
int count; //当前字符串出现次数
struct DicTrie *next[MAX ]; //每个节点 最多 有 MAX 个孩子节点 结构体嵌套
} ; DicTrie *root = NULL; // 根节点是一个空节点 特此声明并赋值 int init() //初始化链表,有对空间要求的 可选用该方法进行初始化
{
root = new DicTrie;
root->count = 0;
for (int i = 0; i < MAX; i ++) {
root->next[i] = NULL; // 空节点
root->isTerminal = false; //
}
return 0;
} bool isFindTrie(char *targetString) //判断是否能在字典树中查到目标串
{
int len = strlen(targetString);
DicTrie *head = root;
for (int i = 0; i < len; i ++) {
int res = (int)(targetString[i] - 'a');//当前小写字母对应数字
if (head->next[res] == NULL) { //如果是空节点 则为否 结束查找
return false;
} else {//不为空 更新头指针 以便继续往下查找
head = head->next[res];
}
}
if (head->count > 0) {
return true;
}
return false;
} int findTrieCount(char *targetString) //判断是否能在字典树中查到目标串
{
int len = strlen(targetString);
DicTrie *head = root;
for (int i = 0; i < len; i ++) {
int res = (int)(targetString[i] - 'a');//当前小写字母对应数字
if (head->next[res] == NULL) { //如果是空节点 则为否 结束查找
return false;
} else {//不为空 更新头指针 以便继续往下查找
head = head->next[res];
}
}
return head->count;
} int insertTrie(char *targetString)
{
int len = strlen(targetString);
DicTrie *head = root;
for (int i = 0; i < len; i ++) {
int res = (int)(targetString[i] - 'a');//当前小写字母对应数字
if (head->next[res] == NULL) { //如果是空节点
head->next[res] = new DicTrie;//则插入新节点元素
head = head->next[res]; //更新头指针 并初始化
head->count = 0; //
for (int j = 0; j < MAX; j ++) {
head->next[j] = NULL;
head->isTerminal = false;
}
} else {
head = head->next[res];
}
}
head->count ++;//每次插入一个,响应计数都增加1
head->isTerminal = true;
return head->count;
} int deleteTrie(char *targetString)
{
int len = strlen(targetString);
DicTrie *head = root;
for (int i = 0; i < len; i ++) {
int res = (int)(targetString[i] - 'a');//当前小写字母对应数字
if (head->next[res] == NULL) { //如果是空节点 表示删除的字符串不在字典中
return 0;
} else { //继续查找
head = head->next[res];
}
}
head->count --;//每次删除一个,响应计数都-1
if (head->count <= 0) {
head->count = 0;
head->isTerminal = false;
}
return 0;
} int main(int argc, const char * argv[]) {
// insert code here...
int n;
char targetString[20]; init(); scanf("%d",&n); //n 组数据
for (int i = 0; i < n; i ++) {
scanf("%s",targetString);
//字符串插入字典树
insertTrie(targetString);//插入方法
}
scanf("%s",targetString);
printf("==%d\n",findTrieCount(targetString));//查找方法 return 0;
}
参考
字典树 trie树 学习的更多相关文章
- 字典树(Trie树)的实现及应用
>>字典树的概念 Trie树,又称字典树,单词查找树或者前缀树,是一种用于快速检索的多叉树结构,如英文字母的字典树是一个26叉树,数字的字典树是一个10叉树.与二叉查找树不同,Trie树的 ...
- [POJ] #1002# 487-3279 : 桶排序/字典树(Trie树)/快速排序
一. 题目 487-3279 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 274040 Accepted: 48891 ...
- Atitit 常见的树形结构 红黑树 二叉树 B树 B+树 Trie树 attilax理解与总结
Atitit 常见的树形结构 红黑树 二叉树 B树 B+树 Trie树 attilax理解与总结 1.1. 树形结构-- 一对多的关系1 1.2. 树的相关术语: 1 1.3. 常见的树形结构 ...
- 洛谷$P4585\ [FJOI2015]$火星商店问题 线段树+$trie$树
正解:线段树+$trie$树 解题报告: 传送门$QwQ$ $umm$题目有点儿长我先写下题目大意趴$QwQ$,就说有$n$个初始均为空的集合和$m$次操作,每次操作为向某个集合内加入一个数$x$,或 ...
- luoguP6623 [省选联考 2020 A 卷] 树(trie树)
luoguP6623 [省选联考 2020 A 卷] 树(trie树) Luogu 题外话: ...想不出来啥好说的了. 我认识的人基本都切这道题了. 就我只会10分暴力. 我是傻逼. 题解时间 先不 ...
- 字典树trie的学习与练习题
博客详解: http://www.cnblogs.com/huangxincheng/archive/2012/11/25/2788268.html http://eriol.iteye.com/bl ...
- [转载]字典树(trie树)、后缀树
(1)字典树(Trie树) Trie是个简单但实用的数据结构,通常用于实现字典查询.我们做即时响应用户输入的AJAX搜索框时,就是Trie开始.本质上,Trie是一颗存储多个字符串的树.相邻节点间的边 ...
- Luogu P2922 [USACO08DEC]秘密消息Secret Message 字典树 Trie树
本来想找\(01Trie\)的结果找到了一堆字典树水题...算了算了当水个提交量好了. 直接插入模式串,维护一个\(Trie\)树的子树\(sum\)大小,求解每一个文本串匹配时走过的链上匹配数和终点 ...
- 【字符串算法】字典树(Trie树)
什么是字典树 基本概念 字典树,又称为单词查找树或Tire树,是一种树形结构,它是一种哈希树的变种,用于存储字符串及其相关信息. 基本性质 1.根节点不包含字符,除根节点外的每一个子节点都包含一个字符 ...
随机推荐
- PyCharm创建文件时自动添加头注释
进入设置 File->settings->Editor->File and Code Templates->Python Script 添加以下代码: #!/usr/bin/e ...
- springmvc中action跳转
return "redirect:/activity/listactivity.htm";
- sublime和webstorm安装zencoding
[webstorm] 1.下载模板,地址为http://code.google.com/p/zen-coding/downloads/list,下载以下文件 WebIDE and IntelliJ I ...
- Spring MVC文件上传教程
1- 介绍 这篇教程文章是基于 Spring MVC来实现文件的上传功能,这里主要是实现两个功能:1.上传单个文件并将其移动到对应的上传目录:2.一次上传多个文件并将它们存储在指定文件夹下,接下来我们 ...
- JavaScript 函数语法
函数就是包裹在花括号中的代码块,前面使用了关键词 function: function functionname() { 这里是要执行的代码 } 当调用该函数时,会执行函数内的代码. 可以在某事件发生 ...
- poj 3469(网络流模版)
题目链接:http://poj.org/problem?id=3469 思路:终于把网络流的模版测试好了,在Dinic和Sap之间还是选择了Sap,事实证明Sap确实比Dinic效率高,在此贴出自己的 ...
- 基于ormlite创建数据库存储数据案例
一直不知道安卓创建数据库存储数据,以前遇到过,但是没有深入研究,今天仔细的看了一下,学习到了一点知识 直接看代码了 public class DatabaseHelper extends OrmLit ...
- iphone客户端上传图片到服务器
本文转载至 http://blog.sina.com.cn/s/blog_4c70701801012inq.html 如上采用asihttprequest类中的post方式上传就行.大致思 ...
- iPhone程序中的加密处理
本文转载至 http://blog.csdn.net/zaitianaoxiang/article/details/6650478 原文链接 : http://www.yifeiyang.ne ...
- mysql 存储二进制数据
晚上小研究了下MySQL存储于读取二进制数据的功能.关键步骤为以下三点: 最重要的一点:存储二进制数据的表的类型需要是blob类型(按长度不同分为tiny, media, long) 插入二进制数据时 ...