hdu 1251(字典树)
题目链接: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(字典树)的更多相关文章
- hdu 1251 字典树的应用
这道题看了大神的模板,直接用字典树提交的会爆内存,用stl 里的map有简单有快 #include <iostream> #include <map> #include < ...
- HDU - 1251 字典树模板题
Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). Input输入数据的第一部 ...
- HDU 1251 字典树(前缀树)
题目大意 :Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).(单词互不相同) ...
- hdu 1251 字典树模板题 ---多串 查找单词出现次数
这道题题目里没有给定数据范围 我开了2005 疯狂的WA 然后开了50000, A掉 我以为自己模板理解错 然后一天没吃饭,饿得胃疼还是想着把这题A掉再去吃,谁知竟然是这样的问题,,,呵呵~~~ ...
- HDU 5687 字典树插入查找删除
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5687 2016百度之星资格赛C题,直接套用字典树,顺便巩固了一下自己对字典树的理解 #include< ...
- HDU 5384 字典树、AC自动机
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5384 用字典树.AC自动机两种做法都可以做 #include<stdio.h> #includ ...
- HDU 1251 Trie树模板题
1.HDU 1251 统计难题 Trie树模板题,或者map 2.总结:用C++过了,G++就爆内存.. 题意:查找给定前缀的单词数量. #include<iostream> #incl ...
- hdu 2112(字典树+最短路)
HDU Today Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdu 2072(字典树模板,set,map均可做)
地址:http://acm.hdu.edu.cn/showproblem.php?pid=2072 lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词 ...
随机推荐
- java.io.FileNotFoundException: /home/hadoop/hadoop/dfs/namenode/current/VERSION (Permission denied)
今天布置hadoop集群,尝试单独将secondarynamenode分属到一台独立的虚拟机上, 当格式化后,start-dfs.sh.namenode没启动.查看日志.报错例如以下 查看权限才发现, ...
- 9、Cocos2dx 3.0游戏开发找小三之工厂方法模式与对象传值
重开发人员的劳动成果,转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/27704153 工厂方法模式 工厂方法是程序设计中一个 ...
- li排序
html <ul id="ul1"> <li>9</li> <li>2</li> <li>7</li& ...
- ORA-12012: error on auto execute of job "ORACLE_OCM
ALERT日志中报错例如以下: Sun Mar 30 06:05:40 2014 Errors in file /oracle/app/oracle/diag/rdbms/zscims/zscims1 ...
- Java新手入门的30个基本概念
Java已经成为一个庞大而复杂的技术平台,对于开发者而言,特别是刚開始学习的人,要想更好的掌握Java技术,深入理解基本概念不可缺少,能够帮助你提高对Java的进一步了解.以下为你介绍了Java语言的 ...
- [原创].NET 业务框架开发实战之八 业务层Mapping的选择策略
原文:[原创].NET 业务框架开发实战之八 业务层Mapping的选择策略 .NET 业务框架开发实战之八 业务层Mapping的选择策略 前言:在上一篇文章中提到了mapping,感觉很像在重新实 ...
- RH253读书笔记(3)-Lab 3 Securing Networking
Lab 3 Securing Networking Goal: To build skills with the Netfilter packet filter Sequence 1: Applyin ...
- 使用Canvas和Paint自己绘制折线图
主要用于Canvas一个特别简单的小demo. 能够手动点击看每一个月份的数据.很easy.就是用paint在canvas上画出来的. 主要内容就是计算左边价格的位置,以下日期的位置,三根虚线的位置, ...
- Hadoop-2.2.0中国文献—— Web应用代理
Web应用代理是YARN的一部分. 默认地,它会作为 Resource Manager(RM)的一部分来执行, 可是也能够配置成独立执行的模式.使用代理的原因就是减少通过YARN的web攻击的可能性. ...
- HTM5 之 Canvas save 、restore 恢复画布状态的理解
save是用来保存canvas状态,这句话很关键,意思是指后续对canvas的操作:平移.放缩.旋转.错切.裁剪等可以恢复. 我之前一直没能理解,认为对画布的画线等操作也可以恢复,其实不是这样子的,只 ...