Trie树检索字符串
#include <stdio.h>
#include <stdlib.h>
#include <string.h> typedef struct TrieNode_t
{
char data;
short int end_flag;//字符串完全添加标志位
struct TrieNode_t* child_node[];
} TrieNode; TrieNode root = { }; //添加字符串到树中
void InsertString(char a[], int len)
{
int i;
TrieNode *p = &root;
for (i = ; i < len; i++)
{
int index = a[i] - 'a';
if (p->child_node[index] == )
{
TrieNode *p_child = (TrieNode *)malloc(sizeof(struct TrieNode_t));
if (NULL == p_child)
{
printf("malloc fail\n");
return;
}
p_child->data = a[i];
p->child_node[index] = p_child;
}
p = p->child_node[index];
}
p->end_flag = ;
} //查询字符串,时间复杂度为O(len)
int SearchString(TrieNode root, char a[], int len)
{
int i;
TrieNode *p = &root;
for (i = ; i < len; i++)
{
int index = a[i] - 'a';
if (p->child_node[index] == )
{
return -;
}
p = p->child_node[index];
} if (p->end_flag == )
{
return ;
}
else
{
return -;
}
} int main()
{
char a[] = "helloworld";
char b[] = "gelloworld";
char c[] = "helltworld";
char d[] = "helloworlr";
char e[] = "hello";
InsertString(a, );
printf("%d\n", SearchString(root, e, ));
return ;
}
Trie树检索字符串的更多相关文章
- 利用Trie树对字符串集合进行排序并计算特征值
该算法用于将一组乱序的字符串反序列化到一个Trie树中,这个过程即可视为对字符串进行了一次排序. 还可以通过调用 GetFeatureString 将该 Trie 树重新序列化. #include & ...
- poj 2945 trie树统计字符串出现次数
用记录附加信息的val数组记录次数即可. trie的原理:每个可能出现的字目给一个编号c,那么整个树就是一个c叉树 ch[u][c]表示 节点u走c边过去之后的节点 PS:trie树还有种动态写法,使 ...
- 大规模字符串检索-压缩trie树
本文使用压缩trie树实现字符串检索的功能.首先将字符串通过编码转化为二进制串,随后将二进制串插入到trie树中,在插入过程中同时实现压缩的功能. 字符编码采用Huffman,但最终测试发现不采用Hu ...
- 字典树(Trie树)的实现及应用
>>字典树的概念 Trie树,又称字典树,单词查找树或者前缀树,是一种用于快速检索的多叉树结构,如英文字母的字典树是一个26叉树,数字的字典树是一个10叉树.与二叉查找树不同,Trie树的 ...
- Trie树的二三事QWQ
写在前面 Trie,又称字典树,是一种用于实现字符串快速检索的多叉树结构.Trie的每个结点都拥有若干字符指针,若在插入或检索字符串时扫描到一个字符c,就沿着当前节点的c这个字符指针,走向该指针指向的 ...
- 查找(二)简单清晰的B树、Trie树具体解释
查找(二) 散列表 散列表是普通数组概念的推广.因为对普通数组能够直接寻址,使得能在O(1)时间内訪问数组中的任何位置.在散列表中,不是直接把keyword作为数组的下标,而是依据keyword计算出 ...
- B树、Trie树详解
查找(二) 散列表 散列表是普通数组概念的推广.由于对普通数组可以直接寻址,使得能在O(1)时间内访问数组中的任意位置.在散列表中,不是直接把关键字作为数组的下标,而是根据关键字计算出相应的下标. 使 ...
- Trie树总结
Trie,又经常叫前缀树,字典树等等.它有很多变种,如后缀树,Radix Tree/Trie,PATRICIA tree,以及bitwise版本的crit-bit tree.当然很多名字的意义其实有交 ...
- Trie 树总结
Trie,又经常叫前缀树,字典树等等.它有很多变种,如后缀树,Radix Tree/Trie,PATRICIA tree,以及bitwise版本的crit-bit tree.当然很多名字的意义其实有交 ...
随机推荐
- [Cypress] Test React’s Controlled Input with Cypress Selector Playground
React based applications often use controlled inputs, meaning the input event leads to the applicati ...
- POJ 2762--Going from u to v or from v to u?【scc缩点新建图 && 推断是否是弱连通图】
Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 15755 ...
- POJ3126——Prime Path
非常水的一道广搜题(专业刷水题). .. #include<iostream> #include<cstdio> #include<queue> #include& ...
- 手游server之数据IO进化
这里数据IO是指游戏数据存盘和读取. 假设IO处理不好.server在IO时会导致.游戏卡顿较长的时间,严重影响游戏体验. 近期服务端刚好对IO这一块做了优化,把优化过程记录一下. 一 原始版 刚開始 ...
- BNU 34990 Justice String 2014 ACM-ICPC Beijing Invitational Programming Contest
题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=34990 DEBUG了非常久,还是legal的推断函数写错了... 此题做法.枚举Stri ...
- Linux下用ImageMagick玩图像魔术【转】
本文转载自:http://www.linuxdiyf.com/linux/11680.html 不管你知不知道,现在是一个用ImageMagick的好机会,至少,如果你是一个Linux用户的话.这是一 ...
- hdoj--1176--免费馅饼(动态规划)
免费馅饼 Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u Submit Status D ...
- 找新朋友(hdoj--1286--欧拉函数)
欢迎参加--每周六晚的BestCoder(有米!) 找新朋友 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- 转载【H:JL】用大家的力量来总结一个目录(众人拾柴火焰高)
博客地址:http://www.cnblogs.com/HJL-Blog/p/4459245.html
- C - Ilya and Sticks(贪心)
Problem description In the evening, after the contest Ilya was bored, and he really felt like maximi ...