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,暴力理论上是不行的. ...
随机推荐
- leetcode_question_102 Binary Tree Level Order Traversal
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- mobilebone.js 移动web APP单页切换骨架
轻便体积小 原生无依赖 插件可扩展 设计无限制 动效可定制 动静两相宜 能进亦能退 桌面也兼修 一句话功能简介跟传统网页浏览的差别仅仅在于无刷新! 例如,我们浏览首页,首页上有个如下HTML链接: & ...
- RDLC报表系列(四) 矩阵
继续接上一篇的内容,本文主要是讲矩阵的内容 用到的数据源如下: DataTable dt = new DataTable(); dt.Columns.Add("FiscalYear" ...
- HTML网页制作:[12]使用框架结构之frameset
首先,我希望在你的目录下,有4个网页,各自显示不同的内容. 如图所示: 1.html显示“火影忍者” 2.html显示“英雄联盟” 3.html显示“嵌入式开发.网页开发.安卓开发” 4.html显示 ...
- Oracle文章中常用数据表的描述
desc stud; 名称 空值 类型 ---- -------- ------------ ID NOT NULL NUMBER(38) NAME ...
- Java面试题整理(题目内容非原创)
面试题分类: 1.java 基础面试题 Java基础中对于io 中文件的读.写,util中的list map set这些要分清楚 还有线程.socket 都需要了解下 参考链接:http://blog ...
- Servle原理
这篇博客将以Tomcat为例讲一讲Servlet的原理 Servlet容器 Servlet与Servlet容器的关系举个不恰当的例子就像枪和子弹的关系.而Servlet就是子弹,容器就是枪.子弹都有统 ...
- android 快速创建一个新的线程
要给一个activity做成子线程的模式 第一种:直接创建子线程并启动 private Thread newThread; //声明一个子线程 new Thread() { @Override pub ...
- weiphp 微信公众号用程序来设置指定内容消息回复业务逻辑操作
微信公众号机器人回复设置 在公众号插件里面的Robot- Model- weixinAddonModel.php里面的 reply设置 reply($dataArr,$keywordArr) 解析方法 ...
- Oracle 11g R2安装手册(图文教程)For Windows
1.Oracle 11g R2安装手册(图文教程)For Windows 1.下载Oracle 11g R2 for Windows版本,下载地址如下 官方网站: http://download.or ...