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,暴力理论上是不行的. ...
随机推荐
- 通过 Autostereograms 案例学习 OpenGL 和 OpenCL 的互操作性
引言 在过去的十年里, GPU (图形处理单元)已经从特殊硬件(特供)转变成能够在数值计算领域开辟新篇章的高性能计算机设备. 很多算法能够使用拥有巨大的处理能力的GPU来快速运行和处理大数据量.即使在 ...
- iOS 各种传值方式
属性传值 将A页面所拥有的信息通过属性传递到B页面使用 B页面定义了一个naviTitle属性,在A页面中直接通过属性赋值将A页面中的值传到B页面. A页面DetailViewController.h ...
- oracle em命令行配置及界面按钮乱码问题解决方法
一.配置EM dbconsole db [oracle@rusky ~]$ lsnrctl start [oracle@rusky ~]$ emctl start dbconsoleTZ set to ...
- 响应式布局之BootStrap
本文在于巩固基础 学习bootStrap官网http://getbootstrap.com/ 响应式布局是Ethan Marcotte在2010年5月份提出的一个概念,简而言之,就是一个网站能够兼容多 ...
- Mvc4.0添加商品到Cookie
/// <summary> /// 添加Cookie /// </summary> /// <param name="gc">GoodsToCo ...
- 将 Maven生成的java项目转化为支持 Eclipse IDE的项目
转自: http://www.xuebuyuan.com/1297046.html 将 Maven生成的java项目转化为支持 Eclipse IDE的项目 在前一篇文章中,我们使用maven创建 ...
- 【Android】Fragment如何获取子Fragment
今天搞了个嵌套的Fragment,通过外部的Fragment获取的子Fragment代码: this.navigationBar = (HXKJCargoNavigationView) getFrag ...
- thbgm拆包【in progress】
曾经在网上找过但是没找到过....关于东方系列bgm的格式,最初以为是个加密格式,后来听说是多个wav堆到一块儿的.再后来查到有说可以用GoldWave开的.今天试了试成功了.接下来打算研究一下,不过 ...
- 图的深度优先遍历(DFS) c++ 非递归实现
深搜算法对于程序员来讲是必会的基础,不仅要会,更要熟练.ACM竞赛中,深搜也牢牢占据着很重要的一部分.本文用显式栈(非递归)实现了图的深度优先遍历,希望大家可以相互学习. 栈实现的基本思路是将一个节点 ...
- send()和recv()函数详解
send()函数 int send( SOCKET s, const char FAR *buf, int len, int flags ); 不论是客户还是服务器应用程序都用send函数来向TCP连 ...