trie tree(字典树)
hihocoder题目(http://hihocoder.com/problemset):#1014 trie树
#include <iostream>
using namespace std;
class trieTree
{
public:
trieTree()
{
isword=false;
for (int i=;i<;i++)
{
next[i]=NULL;
}
}
~trieTree()
{
for (int i=;i<;i++)
{
if (next[i]!=NULL)
{
destory(next[i]);
}
}
}
void destory(trieTree* index);
int insertWord(char*);
int searchWord(char*);
int findWord(trieTree* index);
private:
trieTree* next[];
bool isword;
};
int main()
{
int dictionarySize;
trieTree root;
char word[];
cin>>dictionarySize;
int i=;
for(i=;i<dictionarySize;i++)
{
cin>>word;
root.insertWord(word);
}
cin>>dictionarySize;
for(i=;i<dictionarySize;i++)
{
cin>>word;
cout<<root.searchWord(word)<<endl;
} return ;
}
int trieTree::insertWord(char* word)
{
trieTree* index=this;
while(*word)
{
if(!index->next[*word-'a'])
{
index->next[*word-'a']=new trieTree;
}
if (!index->next[*word-'a'])
{
return -;
}
else
{
index=index->next[*word-'a'];
}
word++;
}
index->isword=true;
return ;
}
int trieTree::searchWord(char* word)
{
trieTree* index=this;
int count=;
while(*word)
{
if(index->next[*word-'a']==NULL)
{
return ;
}
index=index->next[*word-'a'];
word++;
}
count=findWord(index);
return count;
}
int trieTree::findWord(trieTree* index)
{
int count=;
if(index->isword)
count++;
for(int i=;i<;i++)
{
if(index->next[i]!=NULL)
{
count+=findWord(index->next[i]);
}
}
return count;
}
void trieTree::destory(trieTree* index)
{
for (int i=;i<;i++)
{
if (index->next[i]!=NULL)
{
destory(index->next[i]);
index->next[i]=NULL;
}
}
delete index;
}
通过编译之后,发现提交上去还是错了,Wrong Answer!
trie tree(字典树)的更多相关文章
- Phone List POJ 3630 Trie Tree 字典树
Phone List Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 29416 Accepted: 8774 Descr ...
- Trie(字典树)
没时间整理了,老吕又讲课了@ @ 概念 Trie即字典树,又称单词查找树或键树,是一种树形结构,是一种哈希树的变种,典型应用是统计和排序大量的字符串(不限于字符串) Trie字典树主要用于存储字符串, ...
- 【leetcode】208. Implement Trie (Prefix Tree 字典树)
A trie (pronounced as "try") or prefix tree is a tree data structure used to efficiently s ...
- 208 Implement Trie (Prefix Tree) 字典树(前缀树)
实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个方法.注意:你可以假设所有的输入都是小写字母 a-z.详见:https://leetcode.co ...
- [LintCode] Implement Trie 实现字典树
Implement a trie with insert, search, and startsWith methods. Have you met this question in a real i ...
- Trie - leetcode [字典树/前缀树]
208. Implement Trie (Prefix Tree) 字母的字典树每个节点要定义一个大小为26的子节点指针数组,然后用一个标志符用来记录到当前位置为止是否为一个词,初始化的时候讲26个子 ...
- Trie:字典树
简介 \(Trie\),又称字典树或前缀树,是一种有序树状的数据结构,用于保存关联数组,其中的键值通常是字符串. 作用 把许多字符串做成一个字符串集合,并可以对其进行快速查找(本文以求多少个单词是一个 ...
- trie(字典树)原理及C++代码实现
字典树,又称前缀树,是用于存储大量字符串或类似数据的数据结构. 它的原理是利用相同前缀来减少查询字符串的时间. 不同于BST把关键字保存在本结点中,TRIE可以想象成把关键字和下一个结点的指针绑定,事 ...
- LeetCode 208 Implement Trie (Prefix Tree) 字典树(前缀树)
Implement a trie with insert, search, and startsWith methods.Note:You may assume that all inputs are ...
- ACM学习历程—HDU 5536 Chip Factory(xor && 字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题目大意是给了一个序列,求(si+sj)^sk的最大值. 首先n有1000,暴力理论上是不行的. ...
随机推荐
- openfire研究之部署连接管理器(connection manager)
http://blog.sina.com.cn/s/blog_7325f5150101bafh.html 一. Openfire Connection Manager 简介 Openfire Conn ...
- UVA1600 Patrol Robot
题意: 求机器人走最短路线,而且可以穿越障碍.N代表有N行,M代表最多能一次跨过多少个障碍. 分析: bfs()搜索,把访问状态数组改成了3维的,加了个维是当前能跨过的障碍数. 代码: #includ ...
- q.js实现nodejs顺序调用
nodejs的异步调用有时候是最让人头疼的,如何能是一些代码顺序的执行呢,这里和大家分享nodejs的promise 什么是promise promise一个标准,它描述了异步调用的返回结果,包括正确 ...
- ORACLE 查询表定义
很多文章使用DESC tablename查看,这是查看的表结构,不是表定义. 如下: 1.set long 99999; --增大输出缓冲区 2.SELECT dbms_metadata.get_d ...
- html css float left与 float right的使用说明
点评: CSS中很多时候会用到浮动来布局,也就是经常见到的float:left或者float:right,简单点来说,前者是左浮动(往左侧向前边的非浮动元素飘,全是飘得元素的话,就按照流式来浮动从左到 ...
- 用OpenCV实现Otsu算法
算法的介绍 otsu法(最大类间方差法,有时也称之为大津算法)使用的是聚类的思想,把图像的灰度数按灰度级分成2个部分,使得两个部分之间的灰度值差异最大,每个部分之间的灰度差异最小,通过方差的计算来寻找 ...
- The Struts dispatcher cannot be found. This is usually caused by using Struts tags without the associated filter. 异常
异常信息如下: The Struts dispatcher cannot be found. This is usually caused by using Struts tags without t ...
- Three.js学习笔记 – “我和小伙伴都惊呆了”的特效和Three.js初探
什么是Three.js three.js是JavaScript编写的WebGL第三方库.提供了非常多的3D显示功能.Three.js 是一款运行在浏览器中的 3D 引擎,你可以用它创建各种三维场景,包 ...
- Javascript 拖拽的一些高级的应用——逐行分析代码,让你轻松了解拖拽的原理
我们看看之前的拖拽在周围有东西的时候会出现什么问题? 在高级浏览器中不会有啥问题,我们放到IE7下面测试一下,问题就出来了.如图 我们可以很清楚的看到,文字都已经被选中了.那这个用户体验很不好,用起来 ...
- MySQL操作类的封装(PHP)
<?php class mysql{ /** * 报错函数 * * @param string $error */ function err($error){ die("对不起,您的操 ...