Trie
字典树
class Trie {
public:
Trie() {
root = new Node();
} ~Trie() {
destroy(root);
} void insert(string word) {
Node* next = root;
for (int i = ; i < word.length(); ++i) {
if (next->next[word[i] - 'a'] == NULL) {
next->next[word[i] - 'a'] = new Node();
}
next = next->next[word[i] - 'a'];
}
next->val++; // 只要最尾个字符的位置计数
} int search(string word) {
Node* next = root;
int i = ;
for (; i < word.length() && next != NULL; ++i) {
next = next->next[word[i] - 'a'];
}
if (i == word.length() && next != NULL) {
return next->val;
} else {
return ;
}
} private:
struct Node {
int val;
Node *next[];
Node(): val() {
memset(next, , sizeof(Node*) * );
}
}; void destroy(Node* p) {
if (p == NULL) return;
for (int i = ; i < ; ++i) {
destroy(p->next[i]);
}
delete p;
}
Node* root;
};
Trie的更多相关文章
- 基于trie树做一个ac自动机
基于trie树做一个ac自动机 #!/usr/bin/python # -*- coding: utf-8 -*- class Node: def __init__(self): self.value ...
- 基于trie树的具有联想功能的文本编辑器
之前的软件设计与开发实践课程中,自己构思的大作业题目.做的具有核心功能,但是还欠缺边边角角的小功能和持久化数据结构,先放出来,有机会一点点改.github:https://github.com/chu ...
- [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- hihocoder-1014 Trie树
hihocoder 1014 : Trie树 link: https://hihocoder.com/problemset/problem/1014 题意: 实现Trie树,实现对单词的快速统计. # ...
- 【BZOJ-2938】病毒 Trie图 + 拓扑排序
2938: [Poi2000]病毒 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 609 Solved: 318[Submit][Status][Di ...
- Poj The xor-longest Path 经典题 Trie求n个数中任意两个异或最大值
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5646 Accepted: 1226 Description In an ...
- 二分+DP+Trie HDOJ 5715 XOR 游戏
题目链接 XOR 游戏 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- 【hihoCoder】1036 Trie图
题目:http://hihocoder.com/problemset/problem/1036 给一个词典dict,词典中包含了一些单词words.要求判断给定的一个文本串text中是否包含这个字典中 ...
- 萌新笔记——C++里创建 Trie字典树(中文词典)(一)(插入、遍历)
萌新做词典第一篇,做得不好,还请指正,谢谢大佬! 写了一个词典,用到了Trie字典树. 写这个词典的目的,一个是为了压缩一些数据,另一个是为了尝试搜索提示,就像在谷歌搜索的时候,打出某个关键字,会提示 ...
- 洛谷P2412 查单词 [trie树 RMQ]
题目背景 滚粗了的HansBug在收拾旧英语书,然而他发现了什么奇妙的东西. 题目描述 udp2.T3如果遇到相同的字符串,输出后面的 蒟蒻HansBug在一本英语书里面找到了一个单词表,包含N个单词 ...
随机推荐
- Greedy:Stall Reservations(POJ 3190)
牛挤奶 题目大意:一群牛很挑剔,他们仅在一个时间段内挤奶,而且只能在一个棚里面挤,不能与其他牛共享地方,现在给你一群牛,问你如果要全部牛都挤奶,至少需要多少牛棚? 这一题如果把时间区间去掉,那就变成装 ...
- 【数据结构】hanoi
#include<stdio.h> void hanoi(int n,char x,char y,char z) { ; ) printf("%d. Move disk %d f ...
- HDU 5514 Frogs (容斥原理+因子分解)
题目链接 题意:有n只青蛙,m个石头(围成圆圈).第i只青蛙每次只能条ai个石头,问最后所有青蛙跳过的石头的下标总和是多少? 题解:暴力肯定会超时,首先分解出m的因子,自己本身不用分,因为石头编号是0 ...
- MFC基于Dialog的工程中使用OSG
osg的例子有osgviewerMFC,是MDI类型的MFC工程,我一般用基于对话框的MFC较多. 注意观察MFC_OSG.h文件中的cOSG构造函数,参数是一个窗口句柄hWnd,这里的窗口可以不只局 ...
- 对象复制、克隆、深度clone
-------------------------------------------------------------------------------- ------------------- ...
- Fresco 源码分析(一) DraweeView-DraweeHierarchy-DraweeController(MVC) DraweeHierachy+DraweeController的分析
4.1.5.2 模型层DraweeHierachy继承体系以及各个类的作用 DraweeHierachy (I) --| SettableDraweeHierarchy (I) ------| Gen ...
- SQL 查询CET使用领悟
用到sql的遍历循环查询,如果不考虑用CET,估计又到了自己造轮子的时代了,现在觉得sql的CET确实是个好东西,针对SQL的递归查询,很是不错的方法: with etcRecommandINfo2( ...
- sdut 2413:n a^o7 !(第三届山东省省赛原题,水题,字符串处理)
n a^o7 ! Time Limit: 1000MS Memory limit: 65536K 题目描述 All brave and intelligent fighters, next you w ...
- 11、使用 WinAppDeployCmd 部署appx 包到 Windows10 Mobile上(更新)
在 Windows10 Mobile开发工具里,微软没有提供 wp8 sdk 中 Application Deployment 一样的部署工具,参考 了一下 StackOverflow 论坛上的帖子 ...
- Android Inflate
inflate就相当于将一个xml中定义的布局找出来. 三种方式可以生成LayoutInflater: LayoutInflaterinflater=LayoutInflater.from(this) ...