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最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词 ...
随机推荐
- cocos2d之Box2D详细说明 鼠标联合实现
cocos2d之Box2D具体解释 鼠标关节实现 DionysosLai2014-5-7 我们常常要移动物理世界中的某个物体,例如说石头.木块等.假设我们直接改变这些物体的位置,让这些物体尾随我们手指 ...
- 【v2.x OGE课程 14】 控制使用
在这里,精灵.动画精灵.button天才.经常使用的文本的使用 一个.相关精灵 1.加入精灵 //创建精灵 Sprite bar_up = new Sprite(400, 0, RegionRes.g ...
- Mono for Andriod学习与实践(1)— 初体验
对于Andriod的开发者来说,相信Java语言是第一选择,可是对于.Net开发者来说,要想利用C#在Andriod平台上开发,Mono提供了相应的开发平台来实现,Mono for Andriod就是 ...
- python udp编程实例
与python tcp编程控制见 http://blog.csdn.net/aspnet_lyc/article/details/39854569 c++ udp/tcp 编程见 http://blo ...
- Partition List -- LeetCode
原题链接: http://oj.leetcode.com/problems/partition-list/ 这是一道链表操作的题目,要求把小于x的元素按顺序放到链表前面.我们仍然是使用链表最经常使用 ...
- STL源代码剖析(一) - 内存分配
Allocaor allocator 指的是空间配置器,用于分配内存.STL中默认使用SGI STL alloc作为STL的内存分配器,尽管未能符合标准规格,但效率上更好.SGI STL也定义有一个符 ...
- opencv2对于读书笔记——背投影图像的直方图来检测待处理的内容
一些小的概念 1.直方图是图像内容的一个重要特性. 2.假设一幅图像的区域中显示的是一种独特的纹理或是一个独特的物体,那么这个区域的直方图能够看作是一个概率函数,它给出的是某个像素属于该纹理或物体的概 ...
- Chapter 1 Securing Your Server and Network(9):使用Kerberos用于身份验证
原文:Chapter 1 Securing Your Server and Network(9):使用Kerberos用于身份验证 原文出处:http://blog.csdn.net/dba_huan ...
- TCP与UDP的侵略性
HTTP必须执行在TCP上吗?SSL必须执行在TCP上吗?...实际上HTTP并没有规定一定要执行在TCP上,甚至FTP也不一定要执行在TCP上!HTTP或者FTP仅仅是说底层信道要保证数据的按序传输 ...
- jxl创Excel档java示例代码说明
记得下载 并 导入jxl.jar 包,免积分下载地址:http://download.csdn.net/detail/u010011052/7561041 package Test; import j ...