一、题意

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

二、分析

根据题目很容易想到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. css3 练习

    css3 文本效果 css3中包含几个新的文本特征 在本章中您将了解一下文本属性 text-shadow box-shadow word-wrap word-break css3 的文本阴影 css3 ...

  2. spring的父子关系

    1.父容器不能拿子容器的资源 2.子容器可以拿到父容器的资源

  3. javascript内置函数

    1.Date:日期函数属性(1):constructor 所修立对象的函数参考prototype 能够为对象加进的属性和方法办法(43):getDay() 返回一周中的第几天(0-6)getYear( ...

  4. include 语句中使用双引号与括号有什么区别?

    Include 的语法 你在学习如何构造函数时,看到了不同的 include 语句: # include <iostream> # include "distance.h&quo ...

  5. es6 中let与const的简析

    1.let 它的作用类似于var,用来声明变量,但是所声明的变量,只在let命令所在的代码块内有效. if(true){ ; let b = ; } document.write(a); docume ...

  6. 我为什么飞行 10000 公里去西班牙参加 KubeCon?

    2019 年 5 月 20 日至 23 日, 由 Cloud Native Computing Foundation (CNCF) 主办的云原生技术大会 KubeCon + CloudNativeCo ...

  7. H3C 常用信息查看命令

  8. behavior planning——11 create a cost function speed penalty

    A  key part of getting transitions to happen when we want  them to is the design of reasonable cost ...

  9. @hdu - 6584@ Meteor

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 询问第 k 小的分子分母 ≤ n 的既约分数. Input 第一 ...

  10. hdu 5745 La Vie en rose(2016多校第二场)

    La Vie en rose Time Limit: 14000/7000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...