字典树 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.根节点不包含字符,除根节点外的每一个子节点都包含一个字符 ...
随机推荐
- 2017 Wuhan University Programming Contest (Online Round) C. Divide by Six 分析+模拟
/** 题目:C. Divide by Six 链接:https://oj.ejq.me/problem/24 题意:给定一个数,这个数位数达到1e5,可能存在前导0.问为了使这个数是6的倍数,且没有 ...
- 2017 Wuhan University Programming Contest (Online Round) B Color 树形dp求染色方法数
/** 题目:Color 链接:https://oj.ejq.me/problem/23 题意:给定一颗树,将树上的点最多染成m种颜色,有些节点不可以染成某些颜色.相邻节点颜色不同.求染色方法数. 思 ...
- jq 获取select text
var Reply_type_name=$("#Reply_type").find("option:selected").text();
- 面向Internet的编程
面向Internet的编程 1994年秋天我返回工作时,这个公司的景象已经完全改变.他们决定Oak语言——跨平台的.安全的.易传输的代码——时理想的面向Internet的语言.同时他们在制作名为Web ...
- sqoop 从oracle导数据到hive中,date型数据时分秒截断问题
oracle数据库中Date类型倒入到hive中出现时分秒截断问题解决方案 1.问题描述: 用sqoop将oracle数据表倒入到hive中,oracle中Date型数据会出现时分秒截断问题,只保留了 ...
- day17 内置函数
一.内置函数接下来,我们就一起来看看python里的内置函数 分类图 1.作用域函数 基于字典的形式获取局部变量和全局变量 globals()——获取全局变量的字典 locals()——获取执行本方法 ...
- 第0周---python网络爬虫前奏
目标:掌握定向网络数据爬取和网页解析的基本能力 Python开发工具的选择
- 拍照权限,GPS权限的控制
最近项目中会遇到一些手机用户权限的问题,从网上百度了一下,发现有一些方法不能解决判断用户权限的是否开关,下面我就介绍两种权限的判断 1 拍照的权限控制 public static boolean is ...
- 50、Toast自定义布局
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http: ...
- CODEVS 必做题:3149、2821、1531、3369、1230
3149 爱改名的小融 2 时间限制: 2 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description Wikioi上有个人叫小融,他喜 ...