一、题意

给出一组单词,另给出一组单词用作查询,求解对于每个用于查询的单词,前一组中有多少个单词以其为前缀。

二、分析

根据题目很容易想到hash的方法,首先可以朴素的考虑将第一组中的所有单词的前缀利用map进行统计,查询时直接得到结果

所以很容易可以得到以下代码。

注意:输入时的空行代表第一行的结束,利用gets读入并根据字符串的长度是否为0来判断;

 # include <iostream>
# include <cstdio>
# include <cstring>
# include <map>
using namespace std;
const int maxn = ;
char word[maxn];
map<string,int> M;
int main()
{
while(gets(word))
{
if(strlen(word) == )
break;
int L = strlen(word);
for(int i=L;i>;i--)
{
word[i] = '\0';
M[word]++;
}
}
while(gets(word))
printf("%d\n",M[word]);
return ;
}

然而,有些题目会卡map的用法,需要找到更高效的算法,这里可以使用Trie树,也就是前缀树、字典树来解决问题。

字典树的根节点为空,用边来表示字母,用根节点到某个节点的路径表示一个单词,字典树可以用数组表示,其中Tree[root][i]=k表示树上位置标号为root的节点的第i个子节点的位置编号为k

 # include <iostream>
# include <cstdio>
# include <cstring>
using namespace std;
const int maxn = 2e6+;
int Tree[maxn][],sum[maxn];
int cnt;
void Insert(char word[])
{
int L = strlen(word);
int root = ;
for(int i=;i<L;i++)
{
int id = word[i] - 'a';
if(!Tree[root][id])
Tree[root][id] = ++cnt;
sum[Tree[root][id]]++;
root = Tree[root][id];
}
}
int Find(char word[])
{
int L = strlen(word);
int root = ;
for(int i=;i<L;i++)
{
int id = word[i] - 'a';
if(!Tree[root][id])
return ;
root = Tree[root][id];
}
return sum[root];
}
int main()
{
char word[];
cnt = ;
while(gets(word))
{
if(strlen(word) == )
break;
Insert(word);
}
while(scanf("%s",word)!=EOF)
printf("%d\n",Find(word));
return ;
}

注意,如果第二组单词用scan读入记得写!=EOF的形式

HDU1251 统计难题[map的应用][Trie树]的更多相关文章

  1. HDU-1251 统计难题,字典树或者map!

    统计难题 很久就看过这个题了,但不会~~~不会~~ 题意:给出一张单词表,然后下面有若干查询,每次给出一个单词,问单词表中是否存在以这个单词为前缀的单词,输出数量.本身也是自身的前缀.只有一组数据! ...

  2. HDU1251 统计难题 【trie树】

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

  3. HDU1251 统计难题(Trie)

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

  4. HDU-1251 统计难题(我就是不用字典树)

    统计难题 ?戳这里可以前往原题 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为 ...

  5. HDU1251统计难题(水字典树)

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

  6. hdu1251统计难题(trie)

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

  7. hdu1251 统计难题

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=1251 题目: 统计难题 Time Limit: 4000/2000 MS (Java/Othe ...

  8. hdu1251(统计难题)

    这题就是一个字典树的模板题 统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Othe ...

  9. 一个简单的统计问题(解决方案:Trie树)

    题目如图   输入几个不重复的单词和几个前缀,分别统计出单词中包含前缀的个数. Trie树   这个题目用到了 Trie 树.它在百度百科中的定义如下:在计算机科学中,Trie,又称字典树.单词查找树 ...

随机推荐

  1. Vue CLI图形化创建项目

  2. mysql 表名和字段、备注

    select t1.table_schema ,t1.table_name ,t2.ordinal_position ,t2.column_name ,t2.data_type ,t2.charact ...

  3. oracle函数 COALESCE(c1, c2, ...,cn)

    [功能]返回列表中第一个非空的表达式,如果所有表达式都为空值则返回1个空值 [参数]c1, c2, ...,cn,字符型/数值型/日期型,必须类型相同或null [返回]同参数类型 [说明]从Orac ...

  4. Error While Loading Shared Libraries, Cannot Open Shared Object File

    In the "I wish the Internet had an actual correct answer" category comes a question from a ...

  5. java+内存分配及变量存储位置的区别

    Java内存分配与管理是Java的核心技术之一,之前我们曾介绍过Java的内存管理与内存泄露以及Java垃圾回收方面的知识,今天我们再次深入Java核心,详细介绍一下Java在内存分配方面的知识.一般 ...

  6. docker + jenkins 自动化部署

    公司书架上有本docker的书籍,正好最近事不多就写个demo来玩一玩. DevOps未死,ContainerOps已到 ContainerOps VS DevOps 避免了复杂的环境,应用之间的相互 ...

  7. 学习layui框架

    Layui是一款功能齐全的前端框架,需要引入对应的CSS文件和JS文件,附属官网链接:Layui官网

  8. jQuery+css3实现极具创意的罗盘旋转时钟效果源码

    效果 HTML代码 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> < ...

  9. js数组冒泡排序

    文章地址 https://www.cnblogs.com/sandraryan/ js数组的冒泡排序是最经典的一种排序方式(我以为). 冒泡排序是吧一组数组的元素两两比较,交换位置,通过多轮比较,实现 ...

  10. H3C 在接口上应用ACL