题目链接:http://acm.hdu.edu.cn/status.php?user=NYNU_WMH&pid=1251&status=5

Trie树的基本实现

字母树的插入(Insert)、删除( Delete)和查找(Find)都非常简单,用一个一重循环即可,即第i 次循环找到前i 个字母所对应的子树,然后进行相应的操作。实现这棵字母树,我们用最常见的数组保存(静态开辟内存)即可,当然也可以开动态的指针类型(动态开辟内存)。至于结点对儿子的指向,一般有三种方法:

1、对每个结点开一个字母集大小的数组,对应的下标是儿子所表示的字母,内容则是这个儿子对应在大数组上的位置,即标号;

2、对每个结点挂一个链表,按一定顺序记录每个儿子是谁;

3、使用左儿子右兄弟表示法记录这棵树。

三种方法,各有特点。第一种易实现,但实际的空间要求较大;第二种,较易实现,空间要求相对较小,但比较费时;第三种,空间要求最小,但相对费时且不易写。

表示用G++提交一直内存超限.....Orz   而C++直接秒过.......srO

malloc 较为费时  124MS

 #include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <ctype.h>
#include <iomanip>
#include <queue>
#include <stdlib.h>
using namespace std; struct node
{
int count;
struct node *next[];
}; struct node *root;
struct node *newset() //
{
struct node *current;
current=(struct node *)malloc(sizeof(struct node));
for(int i= ;i < ; i++){
current->next[i]=NULL;
}
current->count=;
return current;
} void insert(char *s) //
{
struct node *current;
int len=strlen(s);
if(len==)
return ;
current= root;
for(int i= ;i < len; i++){
if(current->next[s[i]-'a']!=NULL){
current = current->next[s[i]-'a'];
current->count = current->count+;
}
else{
current->next[s[i]-'a'] = newset();
current = current->next[s[i]-'a'];
}
}
} int find(char *s)
{
struct node *current;
int len=strlen(s);
if(len==)
return ;
current=root;
for(int i= ;i < len; i++){
if(current->next[s[i]-'a']!=NULL){
current = current->next[s[i]-'a'];
}
else{
return ;
}
}
return current->count;
} int main()
{
char str[];
int i,ans;
root=newset();
while(gets(str)&&str[]!='\0'){
insert(str);
}
while(~scanf("%s",str)){
ans=find(str);
printf("%d\n",ans);
}
return ;
}

先分配好内存      109MS

 #include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <ctype.h>
#include <iomanip>
#include <queue>
#include <stdlib.h>
using namespace std; typedef struct node
{
int count;
struct node *next[];
}Trienode; Trienode *root;
Trienode memory[];
int p=; struct node *newset() //
{
Trienode *current = &memory[p++];
for(int i= ;i < ; i++){
current->next[i]=NULL;
}
current->count=;
return current;
} void insert(char *s) //
{
struct node *current;
int len=strlen(s);
if(len==)
return ;
current= root;
for(int i= ;i < len; i++){
if(current->next[s[i]-'a']!=NULL){
current = current->next[s[i]-'a'];
current->count = current->count+;
}
else{
current->next[s[i]-'a'] = newset();
current = current->next[s[i]-'a'];
}
}
} int find(char *s)
{
struct node *current;
int len=strlen(s);
if(len==)
return ;
current=root;
for(int i= ;i < len; i++){
if(current->next[s[i]-'a']!=NULL){
current = current->next[s[i]-'a'];
}
else{
return ;
}
}
return current->count;
} int main()
{
char str[];
int i,ans;
root=newset();
while(gets(str)&&str[]!='\0'){
insert(str);
}
while(~scanf("%s",str)){
ans=find(str);
printf("%d\n",ans);
}
return ;
}

hdu 1251(字典树)的更多相关文章

  1. hdu 1251 字典树的应用

    这道题看了大神的模板,直接用字典树提交的会爆内存,用stl 里的map有简单有快 #include <iostream> #include <map> #include < ...

  2. HDU - 1251 字典树模板题

    Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).  Input输入数据的第一部 ...

  3. HDU 1251 字典树(前缀树)

    题目大意 :Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).(单词互不相同) ...

  4. hdu 1251 字典树模板题 ---多串 查找单词出现次数

    这道题题目里没有给定数据范围 我开了2005  疯狂的WA 然后开了50000, A掉  我以为自己模板理解错  然后一天没吃饭,饿得胃疼还是想着把这题A掉再去吃,谁知竟然是这样的问题,,,呵呵~~~ ...

  5. HDU 5687 字典树插入查找删除

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5687 2016百度之星资格赛C题,直接套用字典树,顺便巩固了一下自己对字典树的理解 #include< ...

  6. HDU 5384 字典树、AC自动机

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5384 用字典树.AC自动机两种做法都可以做 #include<stdio.h> #includ ...

  7. HDU 1251 Trie树模板题

    1.HDU 1251 统计难题  Trie树模板题,或者map 2.总结:用C++过了,G++就爆内存.. 题意:查找给定前缀的单词数量. #include<iostream> #incl ...

  8. hdu 2112(字典树+最短路)

    HDU Today Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  9. hdu 2072(字典树模板,set,map均可做)

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=2072 lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词 ...

随机推荐

  1. QoS令牌桶工作原理

    QoS的一个重要作用就是对port流量进行监管,也就是限制port流量.但QoS是怎样做到这点的呢?那就是QoS的令牌桶机制了.以下是在笔者刚刚出版的<Cisco/H3C交换机高级配置与管理技术 ...

  2. UI —— 计算器

    #import <UIKit/UIKit.h> @interface MyViewController :UIViewController { NSInteger _firstName; ...

  3. 玩转Web之easyui(二)-----easy ui 异步加载生成树节点(Tree),点击树生成tab(选项卡)

    关于easy ui 异步加载生成树及点击树生成选项卡,这里直接给出代码,重点部分代码中均有注释 前台: $('#tree').tree({ url: '../servlet/School_Tree?i ...

  4. ASP.NET自定义控件组件开发 第四章 组合控件开发CompositeControl 后篇 --事件冒泡

    原文:ASP.NET自定义控件组件开发 第四章 组合控件开发CompositeControl 后篇 --事件冒泡 CompositeControl  后篇 --事件冒泡 系列文章链接: ASP.NET ...

  5. 分布式服务框架 dubbo/dubbox 入门示例(转)

    dubbo是一个分布式的服务架构,可直接用于生产环境作为SOA服务框架. 官网首页:http://dubbo.io/ ,官方用户指南 http://dubbo.io/User+Guide-zh.htm ...

  6. 读书笔记之SQL注入漏洞和SQL调优

    原文:读书笔记之SQL注入漏洞和SQL调优 最近读了程序员的SQL金典这本书,觉得里面的SQL注入漏洞和SQL调优总结得不错,下面简单讨论下SQL注入漏洞和SQL调优. 1. SQL注入漏洞 由于“' ...

  7. c++内存泄漏处理(积累)

    写c++程序时,常常会出现内存泄漏的问题,这里从网上找了一种非常麻烦的方法:假设想找到每一个cpp文件的内存泄漏,都必须在每一个cpp加上例如以下代码: #include <crtdbg.h&g ...

  8. iOS多用连接、反向协议、安全

    资源 WWDC-2013-Session-708 BlackHat-US-2014-"It Just (Net)works" Understanding Multipeer Con ...

  9. PHP实现协同程序

    于server其中编程.为了实现异步.通常情况下,需要回调.比例如下面的代码 function send($value) { $data = process($value); onReceive($d ...

  10. [WebView其中一项研究]:Web Apps基本介绍

    今天,我们开始了解WebView,以及Web Apps发展,从主要内容Android实际的例子来解释正式文件和后续. (博客地址:http://blog.csdn.net/developer_jian ...