传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1251

统计难题

Time Limit: 4000/2000 MS (Java/Others)

Memory Limit: 131070/65535 K (Java/Others)

Problem Description

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

Input

输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串.

注意:本题只有一组测试数据,处理到文件结束.

Output

对于每个提问,给出以该字符串为前缀的单词的数量.

Sample Input

banana

band

bee

absolute

acm

ba

b

band

abc

Sample Output

2

3

1

0


解题心得:

  1. 标准的字典树,一般字典树就是用来求一个前缀的问题,字典树的建树方式其实和0-1树差不多,可以静态建树也可以动态建树,但是动态建树要注意malloc的运行效率很低,malloc不但要先在内存中去找是否存在可以开辟的空间,而且还会分配额外的字节用来记录内存块的信息。new也差不多。
  2. 对于字典树动态建树不太懂的人可以先去看看怎么手动写链表,建树就是一个和写链表差不多的过程,直接贴代码吧,都是套路很容易看懂的。
  3. 记得在HUD中交题的时候要交C++,G++会MLE,不知道为啥。

动态建树malloc

#include<stdio.h>
#include<cstring>
#include<algorithm>
using namespace std;
struct node
{
int sum;
node *next[26];//字典树看字母大小写来看子节点有多少个
};
node *root,*current,*newnode;
char s[15]; void insert_tree()
{
current = root;
int len = strlen(s);
for(int i=0;i<len;i++)
{
int k = s[i] - 'a';
if(current->next[k] == NULL)//按照树搜索下去找不到这个字母那么建立新的节点
{
newnode = (node *)malloc(sizeof(node));
for(int i=0;i<26;i++)
newnode->next[i] = NULL;//新节点建立之后要将后面的指针指向空
newnode->sum = 1;
current->next[k] = newnode;
current = newnode;
}
else
{
current = current->next[k];
(current->sum)++;//记录在前缀中出现的次数
}
}
} int query()
{
int len = strlen(s);
current = root;//都要从根节点开始找
for(int i=0;i<len;i++)
{
int k = s[i] - 'a';
if(current->next[k] == NULL)//如果指向空那么说明没有以这个字符串为前缀的串
return 0;
current = current->next[k];
}
return current->sum;
} int main()
{
root = (node *)malloc(sizeof(node));//根节点
for(int i=0;i<26;i++)
root->next[i] = NULL;
while(gets(s) && s[0]!='\0')
insert_tree();
while(gets(s) && s[0]!='\0')
{
int ans = query();
printf("%d\n",ans);
}
return 0;
}

动态建树new

/*使用new来写其实和malloc写法差不多*/

#include<stdio.h>
#include<algorithm>
#include<cstring>
using namespace std;
struct node
{
int sum;
node *child[26];
node()
{
for(int i=0;i<26;i++)
child[i] = NULL;
}
};
node *newnode,*current,*root;
char s[15]; void insert_tree()
{
int len = strlen(s);
current = root;
for(int i=0;i<len;i++)
{
int k = s[i] - 'a';
if(current->child[k] == NULL)
{
newnode = new node;
newnode->sum = 1;
current->child[k] = newnode;
current = newnode;
}
else
{
current = current->child[k];
(current->sum)++;
}
}
} int query()
{
int len = strlen(s);
current = root;
for(int i=0;i<len;i++)
{
int k = s[i] - 'a';
if(current->child[k] == NULL)
return 0;
current = current->child[k];
}
return current->sum;
} int main()
{
root = new node;
while(gets(s) && s[0] != '\0')
insert_tree();
while(gets(s) && s[0] != '\0')
{
int ans = query();
printf("%d\n",ans);
}
return 0;
}

静态建树

/*使用静态建树其实就是先开辟空间(数组),
要使用的时候直接用指针指过去就行了,
这样可以避免malloc和new效率低的问题*/
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+100;
struct trie
{
int val;
int sum,next[27];
}tree[maxn<<2];//
int num;
char s[100]; void add_trie()
{
int len = strlen(s),pos = 0;
for(int i=0;i<len;i++)
{
int k = s[i] - 'a';
if(!tree[pos].next[k])
tree[pos].next[k] = num++;
pos = tree[pos].next[k];
tree[pos].sum++;
}
} int find()
{
int len = strlen(s),pos = 0;
for(int i=0;i<len;i++)
{
int k = s[i] - 'a';
if(!tree[pos].next[k])
return 0;
pos = tree[pos].next[k];
}
return tree[pos].sum;
} int main()
{
memset(tree,0,sizeof(tree));
num = 1;
while(gets(s) && s[0] != '\0')
add_trie();
while(gets(s) && s[0] != '\0')
{
int ans = find();
printf("%d\n",ans);
}
return 0;
}

HDU:1251-统计难题(字典树模板,动态建树,静态建树)的更多相关文章

  1. hdu 1251 统计难题 (字典树入门题)

    /******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...

  2. hdu 1251 统计难题 (字典树(Trie)<PS:C++提交不得爆内存>)

    统计难题Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submis ...

  3. hdu 1251 统计难题 字典树第一题。

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

  4. HDOJ/HDU 1251 统计难题(字典树啥的~Map水过)

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

  5. hdu 1251 统计难题(字典树)

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others) Total Subm ...

  6. HDU 1251 统计难题 字典树大水题

    今天刚看的字典树, 就RE了一发, 字典树原理还是很简单的, 唯一的问题就是不知道一维够不够用, 就开的贼大, 这真的是容易MLE的东西啊, 赶紧去学优化吧. HDU-1251 统计难题 这道题唯一的 ...

  7. HDU 1251 统计难题(字典树)

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

  8. HDU 1251统计难题 字典树

    字典树的应用. 数据结构第一次课的作业竟然就需要用到树了!!!这不科学啊.赶紧来熟悉一下字典树. 空间开销太大T T #include<cstdio> #include<cstrin ...

  9. hdu -1251 统计难题(字典树水题)

    http://acm.hdu.edu.cn/showproblem.php?pid=1251 建树之后 查询即可. G++提交 ME不知道为什么,c++就对了. #include <iostre ...

  10. hdoj 1251 统计难题(字典树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 思路分析:该问题要求求出以某个字符串为前缀的单词数目,通过使用字典树,在字典树中添加count记 ...

随机推荐

  1. 51NOD 区间的价值 V2

    http://www.51nod.com/contest/problem.html#!problemId=1674 因为题目要求的只是& 和 | 这两个运算.而这两个运算产生的值是有限的. & ...

  2. C#基础之运行环境

    这是我的第一篇博文,目前这一阶段我的目标是先把C#掌握好,C#是一门语言,是基于C风格语言(C.C++和Java)的特性而设计的.所以在我的博客里,我会记录下我的学习笔记,这不仅仅是笔记,还是所学过的 ...

  3. c#基础2-out-ref

    //out参数要求在方法的内部 ; JiangJin(ref salary1); Console.WriteLine(salary1); Console.ReadKey(); 必须为其赋值 out.r ...

  4. CA、公钥、私钥 概要

    CA.公钥.私钥 概要 现在在开发中遇到一个需求,需要使用tls加密技术,之前并没有了解过,这里来做一个关于CA,公钥,密钥的总结,至于怎么生成这儿就不讲了,如果有机会可以再开一个单章来讲一下. 现在 ...

  5. Java定时器Timer,TimerTask每隔一段时间随机生成数字

    1:java.util.Timer类是一种工具,线程用其安排以后在后台线程中执行的任务.可安排任务执行一次,或者定期重复执行. 2:TimerTask类是由 Timer 安排为一次执行或重复执行的任务 ...

  6. ABAP:parameters的用法

    parameters 1.基础用法 parameters:p0(20) type c. 2.使用DEFAULT后缀为参数指定缺省值. parameters:p1(20) type c default ...

  7. [总结] min-25筛

    再不写总结我又会忘掉啊啊啊啊啊啊啊啊啊 这个\(min-25\)筛主要用来求一个积性函数的前缀和,就像这样\[\sum_{i=1}^n f(i)\] 不过这个积性函数要满足两个条件:质数\(p\)的函 ...

  8. jsHint-静态代码检查工具eclipse中使用

    今天介绍一个关于js静态代码的检查工具,此工具可以帮助更好的规范代码的编写形式以及检查错误.由于jslint的分支jsHint有跟多的配置项相对使用也比较方便,依次本文主要介绍jsHint的使用方式. ...

  9. BeautifulSoup库测试代码

    import requests from bs4 import BeautifulSoup import time headers={ #'User-Agent':'Nokia6600/1.0 (3. ...

  10. ansible 通过堡垒机/跳板机 访问目标机器需求实战(ssh agent forward)

    一. 需求背景: 在我们使用ansible的过程中经常会遇到这样的情况,我们要管理的机器都在内网中,这些内网机器的登录都是通过跳板机或者堡垒机登录.我们的ansible机器不能直接管理到这些后端的机器 ...