https://vjudge.net/problem/HDU-1251

标准的字典树模板题:

也注意一下输入方法:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = ;
struct node
{
int num;
node *next[maxn];
}; //字典树
class Tree{
public:
node *head;
//构造函数
Tree()
{
head = New();
}
//创建一个新节点
node* New()
{
node *p = new node();
for (int i = ; i < maxn; ++i)
{
p->next[i] = NULL;
}
p->num = ;
return p;
}
//插入一个字符串
void insert(char *str)
{
int len = strlen(str);
node *t, *p = head;
for (int i = ; i < len; ++i)
{
int c = str[i] - 'a';
if (p->next[c] == NULL)
{
t = New(); //创建新的节点
p->next[c] = t;
}
p = p->next[c];
p->num++;
}
}
int find(char *str)
{
node *p = head;
int len = strlen(str);
for (int i = ; i < len; ++i)
{
int c = str[i] - 'a';
if (p->next[c] == NULL)
return ; //不存在这样的字符串
p = p->next[c];
}
return p->num;
}
};
int main()
{
char s[];
Tree tree;
while (cin.getline(s, ))
{
if (strlen(s) == || strcmp(s, " ") == )break; //判断是是否为空行
tree.insert(s);
}
while (~scanf("%s", s))
{
printf("%d\n", tree.find(s));
}
}

字典树模板题(统计难题 HDU - 1251)的更多相关文章

  1. (字典树模板)统计难题--hdu--1251

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1251 在自己敲了一遍后终于懂了,这不就用了链表的知识来建立了树,对!就是这样的,然后再查找 代码: #i ...

  2. HDU 1251 统计难题(字典树模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意:给出一些单词,然后有多次询问,每次输出以该单词为前缀的单词的数量. 思路: 字典树入门题. #inc ...

  3. HDU - 1251 字典树模板题

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

  4. CH 1601 - 前缀统计 - [字典树模板题]

    题目链接:传送门 描述给定 $N$ 个字符串 $S_1,S_2,\cdots,S_N$,接下来进行 $M$ 次询问,每次询问给定一个字符串 $T$,求 $S_1 \sim S_N$ 中有多少个字符串是 ...

  5. HDU 2072 - 单词数 - [(有点小坑的)字典树模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2072 Problem Descriptionlily的好朋友xiaoou333最近很空,他想了一件没有 ...

  6. trie树模板(统计难题)

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

  7. AC日记——统计难题 hdu 1251

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

  8. P1184 高手之在一起(字典树模板题,hash算法, map)

    哎,唯一值得说明的是,这道题的输入有bug 先把字典树的算法模板放一下 #include<iostream> #include<cstring> using namespace ...

  9. (模板)hdoj1251(字典树模板题)

    题目链接:https://vjudge.net/problem/HDU-1251 题意:给定一系列字符串之后,再给定一系列前缀,对每个前缀查询以该字符串为前缀的字符串个数. 思路: 今天开始学字典树, ...

随机推荐

  1. [JZOJ5836] Sequence

    Problem 题目链接 Solution 吼题啊吼题! 首先如何求本质不同的子序列个数就是 \(f[val[i]]=1+\sum\limits_{j=1}^k f[j]\) 其中 \(f[i]\) ...

  2. Hyperledger Fabric密码模块系列之BCCSP(四)

    前面说过要找时间介绍一下bccsp包下面的工厂factory,so here it is. 通过factory目前可以获得两类BCCSP实例,一个是上文说的sw,还有一个是通过pkcs11实现的. B ...

  3. WebForm 【Repeater】展示数据

       在 Webform 数据展示中      界面层  : HTLM 业务逻辑层 :只能用 C#  Repeater    重复器  能够用来循环展示数据 具有5种模板  HeaderTemplat ...

  4. 【Java并发编程】14、Thread,线程说明

    线程的状态:New.Runnable.Blocked.Waiting.Timed waiting.Terminated 1. RUNNABLE,对应"就绪"和"运行&qu ...

  5. Django Rest Framework之权限

    基本代码结构 url.py: from django.conf.urls import url, include from app import views urlpatterns = [ url(r ...

  6. JAVA的高并发基础认知 二

    一.JAVA高级并发 1.5JDK之后引入高级并发特性,大多数的特性在java.util.concurrent 包中,是专门用于多线程发编程的,充分利用了现代多处理器和多核心系统的功能以编写大规模并发 ...

  7. js 绘制数学函数

    <!-- <!doctype html> --> <html lang="en"> <head> <meta charset= ...

  8. 2017-12-21 FriceEngine试用与API中文化

    早先就听闻FriceEngine已有中文接口版本, 可惜没有机会尝试. 经原作者 @大笨蛋千里冰封 (Github账号 @ice1000 )建议, 在FriceEngine的DSL封装的接口基础上, ...

  9. 让 Odoo POS 支持廉价小票打印机

    为了测试 Odoo 在实际业务中的实施,我们开了一家(马上要开第二家分店)猪肉店.由于预算有限,在实施 Odoo PoS 的时候采购了一台价格为 85 元的爱宝热敏打印机,结果连上 Odoo Posb ...

  10. python自动化开发-6-常用模块-续1

    json和pickle模块:用于序列化的模块. 序列化:我们把对象(变量)从内存中变成可存储或传输的过程称之为序列化,在Python中叫pickling,在其他语言中也被称之为serializatio ...