字典树 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.根节点不包含字符,除根节点外的每一个子节点都包含一个字符 ...
随机推荐
- c++ virtual 和 pure virtual的区别
参考资料: http://stackoverflow.com/questions/1306778/c-virtual-pure-virtual-explained 验证代码: #include < ...
- tcp3握手,作用,syn攻击
tcp建立链接3次握手: 1.客户端→服务端,发送seq=x,syn=1 2.服务端→客户端,发送seq=y,syn=1,ack=x+1 3.客户端→服务端,发送seq=z,ack=y+1 三次握手作 ...
- Android Studio 使用笔记:给包重命名~~有点水
很简单,选择需要重命名的包,按下 Shift + F6 这时候出现提示,选择 Rename package 输入新的包名,Refactor按钮会变亮,点击就可以了. 注意:这个是重命名一个包名,想做 ...
- RTT常用数据类型
RTT常用数据类型定义在rtdef.h中 /* RT-Thread basic data type definitions */ typedef signed char rt_int8_t; /**& ...
- jQuery 数据操作函数
函数 描述 .clearQueue() 从队列中删除所有未运行的项目. .data() 存储与匹配元素相关的任意数据. jQuery.data() 存储与指定元素相关的任意数据. .dequeue() ...
- Eclipse 重构菜单
Eclipse 重构菜单 使用Eclipse重构 在项目开发中我们经常需要修改类名,但如果其他类依赖该类时,我们就需要花很多时间去修改类名. 但 Eclipse 重构功能可以自动检测类的依赖关系并修改 ...
- 加号选择器(ul>li + li)
<head> <meta charset="UTF-8"> <title>+ selector</title> <style& ...
- EditTextView
package com.egojit.android.sops.views.EditText; import android.content.Context; import android.graph ...
- hdu1754(splay tree 单点更新,成段查询)
题意就是简单的点更新,成段查询. splay tree 果真是常数比较大的log(n)操作. 比线段树还慢了这么多. // // main.cpp // splay // // Created by ...
- hdu2587(递推)
目前做过的最纠结的一道递推题. 情况比较多,比较复杂... 这题最主要的还是要推出当m=2 时和m>2时,用什么方法最优. 给个数据 n=3,m=2 需要48 n=3,m=3 需要81 如果 ...