HDU1251 统计难题[map的应用][Trie树]
一、题意
给出一组单词,另给出一组单词用作查询,求解对于每个用于查询的单词,前一组中有多少个单词以其为前缀。
二、分析
根据题目很容易想到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树]的更多相关文章
- HDU-1251 统计难题,字典树或者map!
统计难题 很久就看过这个题了,但不会~~~不会~~ 题意:给出一张单词表,然后下面有若干查询,每次给出一个单词,问单词表中是否存在以这个单词为前缀的单词,输出数量.本身也是自身的前缀.只有一组数据! ...
- HDU1251 统计难题 【trie树】
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Total Subm ...
- HDU1251 统计难题(Trie)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Total Subm ...
- HDU-1251 统计难题(我就是不用字典树)
统计难题 ?戳这里可以前往原题 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为 ...
- HDU1251统计难题(水字典树)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Total Subm ...
- hdu1251统计难题(trie)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- hdu1251 统计难题
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=1251 题目: 统计难题 Time Limit: 4000/2000 MS (Java/Othe ...
- hdu1251(统计难题)
这题就是一个字典树的模板题 统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Othe ...
- 一个简单的统计问题(解决方案:Trie树)
题目如图 输入几个不重复的单词和几个前缀,分别统计出单词中包含前缀的个数. Trie树 这个题目用到了 Trie 树.它在百度百科中的定义如下:在计算机科学中,Trie,又称字典树.单词查找树 ...
随机推荐
- 通过JS操作CSS
动态效果如图所示: 第一种实现方法: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...
- 2019-3-13-win10-uwp-使用-ScaleTransform-放大某个元素
title author date CreateTime categories win10 uwp 使用 ScaleTransform 放大某个元素 lindexi 2019-3-13 19:5:56 ...
- git操作——TortoiseGit指定某个分支clone
需求 需要使用TortoiseGit 克隆某个项目分支 操作 勾选分支,输入分支名称clone代码即可
- LeetCode108 Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. (M ...
- Ubuntu+Apache+PHP+Mysql环境搭建(完整版)(转)
http://www.2cto.com/os/201505/401588.html Ubuntu+Apache+PHP+Mysql环境搭建(完整版) 一.操作系统Ubuntu 14.04 64位,阿里 ...
- 2019-7-25-VisualStudio-2019-新创建项目添加-git-仓库
title author date CreateTime categories VisualStudio 2019 新创建项目添加 git 仓库 lindexi 2019-7-25 15:8:15 + ...
- Knative 初体验:Eventing Hello World
作者 | 阿里云智能事业群高级开发工程师 元毅 基于事件驱动是Serveless的核心功能之一,通过事件驱动服务,满足了用户按需付费(Pay-as-you-go)的需求.在之前的文章中我们介绍过 Kn ...
- SDUT-3331_数据结构实验之链表八:Farey序列
数据结构实验之链表八:Farey序列 Time Limit: 10 ms Memory Limit: 600 KiB Problem Description Farey序列是一个这样的序列:其第一级序 ...
- 2018-11-19-Roslyn-NameSyntax-的-ToString-和-ToFullString-的区别
title author date CreateTime categories Roslyn NameSyntax 的 ToString 和 ToFullString 的区别 lindexi 2018 ...
- 请求(RequestInfo)
请求类型 StringRequestInfo 用在 SuperSocket 命令行协议中. 你也可以根据你的应用程序的需要来定义你自己的请求类型. 例如, 如果所有请求都包含 DeviceID 信息, ...