一、题意

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

二、分析

根据题目很容易想到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. C++中的union

    1:,像任何类一样,union可以指定保护标记使成员成为公用的.私有的或受保护的.默认情况下,union 表现得像 struct:除非另外指定,否则 union 的成员都为 public 成员. 2: ...

  2. MaxCompute 费用暴涨之存储压缩率降低导致SQL输入量变大

    现象:同样的SQL,每天处理的数据行数差不多,但是费用突然暴涨甚至会翻数倍. 分析: 我们先明确MaxCompute SQL后付费的计费公式:一条SQL执行的费用=扫描输入量 ️ SQL复杂度 ️ 0 ...

  3. 字体图标font-awesome

    其实有一些常见的图标使用字体图标比使用img来得好 Font Awesome 官网:http://fortawesome.github.io/Font-Awesome/ 字体代码:http://for ...

  4. 【NS2】Ubuntu 12.04 LTS 中文输入法的安装(转载)

    本文是笔者使用 Ubuntu 操作系统写的第一篇文章!参考了红黑联盟的这篇文章:Ubuntu 12.04中文输入法的安装 安装 Ubuntu 12.04 着实费力一番功夫,老是在用 Ubuntu 来引 ...

  5. 罗列Python标准模块

    文本 1. string:通用字符串操作 2. re:正则表达式操作 3. difflib:差异计算工具 4. textwrap:文本填充 5. unicodedata:Unicode字符数据库 6. ...

  6. this 、静态变量

    /*作者:qingfeng日期:2017/2/18功能:this,静态变量(类变量)*/class Demo3_2{    public static void main(String args[]) ...

  7. LeetCode75 Sort Colors

    题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...

  8. 从 Apache ORC 到 Apache Calcite | 2019大数据技术公开课第一季《技术人生专访》

    摘要: 什么是Apache ORC开源项目?主流的开源列存格式ORC和Parquet有何区别?MaxCompute为什么选择ORC? 如何一步步成为committer和加入PMC的?在阿里和Uber总 ...

  9. @codeforces - 1186F@ Vus the Cossack and a Graph

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一个 n 点 m 边的图(n, m<=10^6),记第 ...

  10. Project Euler Problem 5-Smallest multiple

    对每个数字分解素因子,最后对每个素因子去其最大的指数,然后把不同素因子的最大指数次幂相乘,得到的就是最小公倍数 python不熟练,代码比较挫 mp = {} def process(n): i = ...