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,暴力理论上是不行的. ...
随机推荐
- Trie树-脏词过滤应用
Trie树,又称字符查找树.前缀树,主要用于字符匹配(详见http://en.wikipedia.org/wiki/Trie).适合做关键词查找,比如查找文章中的关键字然后给他们加链接. 当然对脏词的 ...
- hadoop结构出现后format变态
14/07/10 18:50:47 FATAL conf.Configuration: error parsing conf file: com.sun.org ...
- Android 开源框架ActionBarSherlock 和 ViewPager 仿网易新闻客户端
转载请注明出处:http://blog.csdn.net/xiaanming/article/details/9971721 大家都知道Android的ActionBar是在3.0以上才有的,那么在3 ...
- jQuery源代码学习笔记:jQuery.fn.init(selector,context,rootjQuery)代码具体解释
3.1 源代码 init: function( selector, context, rootjQuery ) { var match, elem, ret, doc; // Handle $(&qu ...
- Sublime 学习记录(一) Sublime 的快捷键
Ctrl + Shift + P : 打开命令面板 Ctrl + P : 搜索项目中的文件 Ctrl + W : 关闭当前打开的文件 Ctrl + G : 跳转到第几行 Ctrl + Shift + ...
- RDLC报表系列(三) 总计和折叠
继续接上一篇的内容,本文主要是简述总计和折叠 1.在Dept列右击,选择插入行->组内部下方(每个部门的Total),插入新的一行,并拆分单元格,然后合并列 3.设置文本框属性,格式为Total ...
- 移动端影像解决方案Adobe Creative SDK for ios
移动端影像解决方案Adobe Creative SDK for ios 2015-12-20 分类:整理 阅读(390) 评论(0) 老牌影像界泰斗不甘落寞,正式推出了Adobe Creative ...
- phpStorm注册马码
phpstorm已经升级到10.0,原注册码失效,10.0注册方法:注册时选择“License server”输入 http://idea.lanyus.com/ 此工具类工具一直没有让我失望
- C语言strcmp()函数:比较字符串(区分大小写)
头文件:#include <string.h> strcmp() 用来比较字符串(区分大小写),其原型为: int strcmp(const char *s1, const char *s ...
- linux自旋锁
一.前言 在linux kernel的实现中,经常会遇到这样的场景:共享数据被中断上下文和进程上下文访问,该如何保护呢?如果只有进程上下文的访问,那么可以考虑使用semaphore或者mutex的锁机 ...