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最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词 ...
随机推荐
- HDU 4288 Coder (线段树)
Coder 题目:http://acm.hdu.edu.cn/showproblem.php?pid=4288 题意:有三种类型的操作,(1)."add x",表示往集合里加入�数 ...
- 猫学习IOS(五岁以下儿童)UI之360其他下载管理器广场UI
猫分享.必须精品 下载材料:http://blog.csdn.net/u013357243/article/details/44486651 先看效果 主要是完毕了九宫格UI的搭建 代码 - (voi ...
- ZOJ3827 ACM-ICPC 2014 亚洲区域赛的比赛现场牡丹江I称号 Information Entropy 水的问题
Information Entropy Time Limit: 2 Seconds Memory Limit: 131072 KB Special Judge Informatio ...
- Object-C 新手教程
大纲 開始吧 下载这篇教学 设定环境 前言 编译 hello world 创建 Classes @interface @implementation 把它们凑在一起 具体说明... 多重參数 建构子( ...
- Android-管理Activity生命周期
用户在浏览,退出,返回app时,app中的Activity实例会在不同状态之间切换.比如,当activity第一次启动,然后来到系统前台,受到用户的注意,这个过程中,android系统调用了一系列ac ...
- FZU1669 Right-angled Triangle【毕达哥拉斯三元组】
主题链接: pid=1669">http://acm.fzu.edu.cn/problem.php?pid=1669 题目大意: 求满足以a.b为直角边,c为斜边,而且满足a + b ...
- Android4.2.2由于越来越多的物理按键(frameworks)
当我们改变frameworks之后可能: make: *** [out/target/common/obj/PACKAGING/checkapi-current-timestamp] 错误 38 解决 ...
- HDU ACM 1065 I Think I Need a Houseboat
分析:告诉协调的房子,每年(0,0)作为一个半圆区域的中心将被添加50.请教如何多年以来,这家的位置将是半圆内.注意pi必须采取3.1415926管辖权. #include<iostream&g ...
- 使用IronPython给.Net程序
使用IronPython给.Net程序加点料 开发的时候,经常被策划频繁变动的方案而苦恼.这时候就想要加入点动态语言来辅助一下. 在考虑用动态语言之前也曾想过使用动态加载dll的方式,实现基础接口来调 ...
- flipsnap.js 源码阅读备份
这是官网:http://hokaccha.github.io/js-flipsnap/ 1.引入全局命名空间 类似jQuery插件写法 传入window, document,提高内部访问速度: ; ...