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. windows 安装 keras

    pip install keras 报错了,看报错信息是卡在scipy上了,查了一下 https://stackoverflow.com/questions/42240720/python-scipy ...

  2. What are the differences between a pointer variable and a reference variable in C++?

    Question: I know references are syntactic sugar, so code is easier to read and write. But what are t ...

  3. 再也不用担心面试官问你HashCode和equals了

    结论 如果两个对象相等,则hashcode()必须相等. 如果两个对象相等,a.equals(b)==b.equals(a)==true 如果两个对象有相同的hashcode值,他们也不一定是相等的. ...

  4. Django + Mysql 中关于时间异常返回500错误的解决

    问题描述: 最近在阿里云部署 Django(1.11.x) 时,在后台发布文章后,页面返回 500 异常. 刚开始的时候,遇到这个问题一脸懵逼,不知道该如何入手.后来把 settings.py 中 D ...

  5. idea not found for the web module

    intellij  IDEA  的tomcat 配置项里面没有的app web:war exploded  没有配置/路径

  6. input上传文件个数控制

    HTML: <h3>请上传[2,5]个文件</h3> <form action="" enctype="multipart/form-dat ...

  7. ajax请求完之前的loading加载

    很多时候我们需要引入框架来开发项目,这时我们可能会遇到页面还没加载完源码出来了的问题,给用户一种不好的视觉体验,这是便需要loading加载了,来完善用户体验! /*loading.js*/ // 加 ...

  8. Vue遇到的一些小坑

    1.在使用v-html指令时,发现添加的元素不能设置样式 解决方案:在添加样式时使用>>>就可以添加成功 例如:<div v-html="<img src=&q ...

  9. 使用jquery获取iframe内的元素属性

    当需要获取iframe里的内容时需要有几个前提,否则你是获取不到的: 1:当前页面与iframe的src的页面需要在同一个域名下: 2:必须要等iframe里边的页面加载完成才能获取,否则你要获取的标 ...

  10. 警告!中国90%AI初创企业将在两年内落败出局

    https://mp.weixin.qq.com/s/-RkyLda1jovaHBlBTsi-BA 近年来,中国涌现了一大批AI初创企业,但AI热潮也伴随着泡沫.由于近期市场资金紧缩,投资者发出警告, ...