HDU1671——前缀树的一点感触
题目http://acm.hdu.edu.cn/showproblem.php?pid=1671
题目本身不难,一棵前缀树OK,但是前两次提交都没有成功。
第一次Memory Limit Exceeded:
前缀树是很费空间的数据结构,每个节点存放了字母(数字)个数个指针,正所谓用空间来换取时间。
我发现我忘记写析构函数了,所以测例多起来还不释放很容易超空间。
树形结构的析构也很有趣:
~Node()
{
for (int i = ; i < ; ++i)
{
if (children[i])
{
delete children[i];
children[i] = nullptr;
}
}
}
好了,第二次没成功,Time Limit Exceeded:
看起来这是一棵规规矩矩的前缀树,仔细找可以优化的地方,试探性地把
vector<Node *> children;
Node()
{
for (int i = ; i < ; ++i) children.push_back(nullptr);
}
改成了
vector<Node *> children = vector<Node *>(, nullptr);
Node()
{
//for (int i = 0; i < 10; ++i) children.push_back(nullptr);
}
Accepted!显然构造的时候赋值比构造之后再赋值要快。。果然还是要尽可能的优化啊!
附代码:
#include <string>
#include <vector>
#include <iostream> using namespace std; class Node
{
public:
bool is_phone = false;
vector<Node *> children = vector<Node *>(, nullptr);
Node()
{}
~Node()
{
for (int i = ; i < ; ++i)
{
if (children[i])
{
delete children[i];
children[i] = nullptr;
}
}
}
void insert(const string &_Phone)
{
Node *p = this;
for (int i = ; i < _Phone.size(); ++i)
{
int curr = _Phone[i] - '';
if (p->children[curr] == nullptr)
{
p->children[curr] = new Node();
}
p = p->children[curr];
}
p->is_phone = true;
}
bool has_phone(const string &_Phone)
{
Node *p = this;
for (int i = ; i < _Phone.size(); ++i)
{
int curr = _Phone[i] - '';
if (!p->children[curr]) return false;
if (p->children[curr]->is_phone) return true;
p = p->children[curr];
}
return true;
}
}; typedef Node *Trie; int main()
{
int t;
cin >> t;
while (t--)
{
Trie trie = new Node();
int n;
cin >> n;
string phone;
bool flag = true;
while (n--)
{
cin >> phone;
if (trie->has_phone(phone)) { flag = false; }
trie->insert(phone);
}
if (flag) cout << "YES" << endl;
else cout << "NO" << endl;
delete trie;
}
//system("pause");
return ;
}
HDU1671——前缀树的一点感触的更多相关文章
- 【Todo】字符串相关的各种算法,以及用到的各种数据结构,包括前缀树后缀树等各种树
另开一文分析字符串相关的各种算法,以及用到的各种数据结构,包括前缀树后缀树等各种树. 先来一个汇总, 算法: 本文中提到的字符串匹配算法有:KMP, BM, Horspool, Sunday, BF, ...
- [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- trie树(前缀树)
问题描述: Trie树,即字典树,又称单词查找树或键树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它的优 ...
- 【暑假】[实用数据结构]前缀树 Trie
前缀树Trie Trie可理解为一个能够快速插入与查询的集合,无论是插入还是查询所需时间都为O(m) 模板如下: +; ; struct Trie{ int ch[maxnode][sigma_siz ...
- [Swift]LeetCode208. 实现 Trie (前缀树) | Implement Trie (Prefix Tree)
Implement a trie with insert, search, and startsWith methods. Example: Trie trie = new Trie(); trie. ...
- 1042.D Petya and Array 前缀 + 树状数组
11.19.2018 1042.D Petya and ArrayNew Point: 前缀 + 树状数组 :树状数组逐个维护前缀个数 Describe: 给你一个数组,一个标记数,问你有多少区间[l ...
- 算法进阶面试题07——求子数组的最大异或和(前缀树)、换钱的方法数(递归改dp最全套路解说)、纸牌博弈、机器人行走问题
主要讲第五课的内容前缀树应用和第六课内容暴力递归改动态规划的最全步骤 第一题 给定一个数组,求子数组的最大异或和. 一个数组的异或和为,数组中所有的数异或起来的结果. 简单的前缀树应用 暴力方法: 先 ...
- 9-11-Trie树/字典树/前缀树-查找-第9章-《数据结构》课本源码-严蔚敏吴伟民版
课本源码部分 第9章 查找 - Trie树/字典树/前缀树(键树) ——<数据结构>-严蔚敏.吴伟民版 源码使用说明 链接☛☛☛ <数据结构-C语言版>(严蔚 ...
- python利用Trie(前缀树)实现搜索引擎中关键字输入提示(学习Hash Trie和Double-array Trie)
python利用Trie(前缀树)实现搜索引擎中关键字输入提示(学习Hash Trie和Double-array Trie) 主要包括两部分内容:(1)利用python中的dict实现Trie:(2) ...
随机推荐
- 【流量劫持】躲避 HSTS 的 HTTPS 劫持
前言 HSTS 的出现,对 HTTPS 劫持带来莫大的挑战. 不过,HSTS 也不是万能的,它只能解决 SSLStrip 这类劫持方式.但仔细想想,SSLStrip 这种算劫持吗? 劫持 vs 钓鱼 ...
- javascript中的事件冒泡和事件捕获
1.事件冒泡 IE 的事件流叫做事件冒泡(event bubbling),即事件开始时由最具体的元素(文档中嵌套层次最深的那个节点)接收,然后逐级向上传播到较为不具体的节点(文档).以下面的HTML ...
- js学习之变量、作用域和内存问题
js学习之变量.作用域和内存问题 标签(空格分隔): javascript 变量 1.基本类型和引用类型: 基本类型值:Undefined, Null, Boolean, Number, String ...
- Java FtpClient 实现文件上传服务
一.Ubuntu 安装 Vsftpd 服务 1.安装 sudo apt-get install vsftpd 2.添加用户(uftp) sudo useradd -d /home/uftp -s /b ...
- 微信小程序IDE(微信web开发者工具)安装、破解手册
1.IDE下载 微信web开发者工具,本人是用的windows 10 x64系统,用到以下两个版本的IDE安装工具与一个破解工具包: wechat_web_devtools_0.7.0_x64.exe ...
- Spring配置文件标签报错:The prefix "XXX" for element "XXX:XXX" is not bound. .
例如:The prefix "context" for element "context:annotation-config" is not bound. 这种 ...
- MySQL Workbench建表时 PK NN UQ BIN UN ZF AI 的含义
[转自网络]https://my.oschina.net/cers/blog/292191 PK Belongs to primary key 作为主键 NN Not Null 非空 UQ Uniqu ...
- thinkphp-无限分类下根据任意部门获取顶级部门ID
根据所得到的部门编号获取顶级部门ID: 参数 - department_id 表格组织架构: tab_departments department_id parent_id name 1 1 顶级 2 ...
- Windows更新清理工具 (winsxs 清理工具)
Windows 更新清理工具是一款效果非常显著的Windows7.Windows8操作系统清理优化工具!经常安装系统的朋友相比有所体会,刚刚安装完成的Win7.Win8其实占的空间并不大,去掉页面文件 ...
- 基于DDDLite的权限管理OpenAuth.net 1.0版正式发布
距离上一篇OpenAuth.net的文章已经有5个多月了,在这段时间里项目得到了很多朋友的认可,开源中国上面的Star数接近300,于是坚定了我做下去的信心.最近稍微清闲点,正式推出1.0版,并在阿里 ...