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最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词 ...
随机推荐
- python正文(两)
在本文中,我读了记录和总结<Python标准库>一本书,本节课文的学习和理解. 事实上,在Python于,使用一些方法这段文字是一回事,尤其是经常使用.在一般情况下,会用String这样的 ...
- Haskell 几乎无疼痛入门指南
当他重装Linux 机会虚拟机,安装 haskell 录制的过程中有什么.的方式来帮助那些谁在徘徊haskell进入外读者. 基本概念: Haskell : 是一门通用函数式语言.差点儿能够进行不论什 ...
- javascript有用小技巧—实现分栏显示
记得给师哥师姐測试考试系统的时候,看到他们的考试页面能够实现隐藏左边的考生信息部分,当时认为好高大上.好人性化. 如今学了javascript,我也能实现这个功能了,以下来显摆一下. 1.页面设计: ...
- Linux 编程学习笔记----过程管理和项目发展(在)
转载请注明出处,http://blog.csdn.net/suool/article/details/38406211,谢谢. Linux进程存储结构和进程结构 可运行文件结构 例如以下图: 能够看出 ...
- [LeetCode145]Binary Tree Postorder Traversal
题目: Given a list, rotate the list to the right by k places, where k is non-negative. For example:Giv ...
- [LeetCode226]Invert Binary Tree
题目: Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 反转二叉树,左右儿子值交换 代码: / ...
- cassandra 服务启动流程
cassandra 服务启动流程 1. setup 1) CassandraDaemon ->main publicstaticvoidmain(String[]args) { insta ...
- [TroubleShooting] The server network address can not be reached or does not exist
Backtround: I'm trying to set up mirroring between two sql 2008 R2 databases on different servers in ...
- 生产都消费者模式的一个demo,消费者设置缓存
package queue; import java.util.concurrent.ExecutorService; import java.util.concurrent.LinkedBlocki ...
- LeetCode——Climbing Stairs
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...