hdu2027 trie树 字典树模板
#include <iostream>
#include <cstdio>
#include <cstring>
#include <sstream>
using namespace std; #define Max 26 const int maxn=; typedef struct TrieNode{
int nCount;//根据需要改变
struct TrieNode *next[Max];
}TrieNode; TrieNode *root=NULL; int allcop=,ans,flag; TrieNode memory[maxn]; TrieNode *CreatTrieNode(){
TrieNode *temp=&memory[allcop++];
temp->nCount=;
for(int i=;i<Max;i++) temp->next[i]=NULL;
return temp;
} void InsertTrie(TrieNode **pRoot,char *str){
TrieNode *temp=*pRoot;
int i=,k;
while(str[i]){
k=str[i]-'a';
if(temp->next[k]){
//temp->next[k]->nCount++;
} else temp->next[k]=CreatTrieNode();
temp=temp->next[k];
i++;
}
temp->nCount++;
} int SearchTrie(TrieNode *root,char *str){
if(root==NULL) return ;
TrieNode *temp=root;
int i=,k;
while(str[i]){
k=str[i]-'a';
if(temp->next[k]){
temp=temp->next[k];
} else return ;
i++;
}
return temp->nCount;
} void Traverse(TrieNode *root){
for(int i=;i<Max;i++){
if(root->next[i]){
if(root->next[i]->nCount>) ans++;
Traverse(root->next[i]);
}
}
} char str[]; void init(){
ans=;
flag=;
allcop=;
memset(memory,,sizeof(memory));
root=CreatTrieNode();
} int main()
{
while(){
init();
string line;
getline(cin,line);
stringstream ss(line);
while(ss>>str) {
if(str[]=='#') {
flag=;
break;
}
InsertTrie(&root,str);
}
if(flag) break;
Traverse(root);
printf("%d\n",ans);
}
return ;
}
输入方式值得学习
hdu2027 trie树 字典树模板的更多相关文章
- 剑指Offer——Trie树(字典树)
剑指Offer--Trie树(字典树) Trie树 Trie树,即字典树,又称单词查找树或键树,是一种树形结构,是一种的单词.对于每一个单词,我们要判断他出没出现过,如果出现了,求第一次出现在第几个位 ...
- AC自动机——1 Trie树(字典树)介绍
AC自动机——1 Trie树(字典树)介绍 2013年10月15日 23:56:45 阅读数:2375 之前,我们介绍了Kmp算法,其实,他就是一种单模式匹配.当要检查一篇文章中是否有某些敏感词,这其 ...
- Trie(字典树)
没时间整理了,老吕又讲课了@ @ 概念 Trie即字典树,又称单词查找树或键树,是一种树形结构,是一种哈希树的变种,典型应用是统计和排序大量的字符串(不限于字符串) Trie字典树主要用于存储字符串, ...
- 9-11-Trie树/字典树/前缀树-查找-第9章-《数据结构》课本源码-严蔚敏吴伟民版
课本源码部分 第9章 查找 - Trie树/字典树/前缀树(键树) ——<数据结构>-严蔚敏.吴伟民版 源码使用说明 链接☛☛☛ <数据结构-C语言版>(严蔚 ...
- Trie:字典树
简介 \(Trie\),又称字典树或前缀树,是一种有序树状的数据结构,用于保存关联数组,其中的键值通常是字符串. 作用 把许多字符串做成一个字符串集合,并可以对其进行快速查找(本文以求多少个单词是一个 ...
- cogs 293. [NOI 2000] 单词查找树 Trie树字典树
293. [NOI 2000] 单词查找树 ★★☆ 输入文件:trie.in 输出文件:trie.out 简单对比时间限制:1 s 内存限制:128 MB 在进行文法分析的时候,通常需 ...
- [LintCode] Implement Trie 实现字典树
Implement a trie with insert, search, and startsWith methods. Have you met this question in a real i ...
- Trie树|字典树(字符串排序)
有时,我们会碰到对字符串的排序,若采用一些经典的排序算法,则时间复杂度一般为O(n*lgn),但若采用Trie树,则时间复杂度仅为O(n). Trie树又名字典树,从字面意思即可理解,这种树的结构像英 ...
- Trie - leetcode [字典树/前缀树]
208. Implement Trie (Prefix Tree) 字母的字典树每个节点要定义一个大小为26的子节点指针数组,然后用一个标志符用来记录到当前位置为止是否为一个词,初始化的时候讲26个子 ...
- Trie树/字典树题目(2017今日头条笔试题:异或)
/* 本程序说明: [编程题] 异或 时间限制:1秒 空间限制:32768K 给定整数m以及n个数字A1,A2,..An,将数列A中所有元素两两异或,共能得到n(n-1)/2个结果,请求出这些结果中大 ...
随机推荐
- Linux_服务器_04_vim编辑器的使用
二.参考文档 1.linux系统中如何进入退出vim编辑器,方法及区别
- Jmeter-聚合报告
线程组右键--添加--监听器--聚合报告 Aggreagete Report:jmeter最常用的一个Listener,“聚合报告”. Label:每个jmeter的element(例如HTTP Re ...
- codeforces 703B B. Mishka and trip(数学)
题目链接: B. Mishka and trip time limit per test 1 second memory limit per test 256 megabytes input stan ...
- python 列表之队列
列表实现队列操作(FIFO),可以使用标准库里的 collections.deque,deque是double-ended quene的缩写,双端队列的意思,它可以实现从队列头部快速增加和取出对象. ...
- Hbase之三:Hbase Shell使用入门
HBase 为用户提供了一个非常方便的使用方式, 我们称之为“HBase Shell”.HBase Shell 提供了大多数的 HBase 命令, 通过 HBase Shell 用户可以方便地创建.删 ...
- warning: conflicting types for built-in function 'puts'
warning: conflicting types for built-in function 'puts' [编译器版本] arm-linux-gcc 3.4.1 [问题描述] 在做嵌入式底层开发 ...
- [Hadoop] Sqoop安装过程详解
Sqoop是一个用来将Hadoop和关系型数据库中的数据相互转移的工具,可以将一个关系型数据库(例如 : MySQL ,Oracle ,Postgres等)中的数据导进到Hadoop的HDFS中,也可 ...
- 附近wifi都是你的
今天给大家介绍deauth攻击. 最终效果:附近你指定的任何wifi,别人都无法连接,即便连接上的也会断掉. 由于我在 “世界虽大,但没有破不了的wifi” 这篇文章中写的很详细,所以我在这里就步详 ...
- Software - (转)Winform 程序捕获全局异常
static class Program { /// <summary> /// 应用程序的主入口点. /// </summary> [STAThread] static vo ...
- gridview把textbox的值修改还是旧值的解决方法
解决方法很简单,加上if(!IsPostBack) 就OK了,因为之前加载之前都会调用InitData(). protected void Page_Load(object sender, Event ...