HDU - 1251 统计难题(Trie树)
有很多单词(只有小写字母组成,不会有重复的单词出现)
要统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).
每个单词长度不会超过10.
Trie树的模板题。这个题内存把控不好容易MLE。
经过某一个节点就记一下插入时经过的次数。那么最后判断一个前缀的时候就看前缀是否能够存在。
如果存在,答案就是最后走到的这个位置记录的数字。
如果不存在,答案就是 0.
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
#define maxn 1000000 + 10
#define sigma_size 27
#define LL long long
#define INF 0x3f3f3f3f int tot = ;
int trie[maxn][sigma_size], sum[maxn]; void insert(char s[])
{
int root = ;
for (int i = ; s[i]; i++)
{
int id = s[i]-'a';
if (!trie[root][id])
trie[root][id] = ++tot;
root = trie[root][id];
sum[root]++;
}
} int found(char s[])
{
int root = ;
for (int i = ; s[i]; i++)
{
int id = s[i]-'a';
if (!trie[root][id])
return ;
root = trie[root][id];
}
return sum[root];
} int main()
{
char s[];
while(gets(s))
{
if (s[] == ) break;
insert(s);
} while(scanf("%s", s) != EOF)
printf("%d\n", found(s));
}
HDU - 1251 统计难题(Trie树)的更多相关文章
- HDU - 1251 统计难题(trie树)
Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). Input输入数据的第一部 ...
- hdu 1251 统计难题(trie树入门)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- hdu 1251 统计难题 (字典树(Trie)<PS:C++提交不得爆内存>)
统计难题Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submis ...
- hdu 1251 统计难题 (字典树入门题)
/******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...
- hdu 1251 统计难题 trie入门
统计难题 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本 ...
- HDOJ/HDU 1251 统计难题(字典树啥的~Map水过)
Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己 ...
- HDU 1251 统计难题(Trie)
统计难题 [题目链接]统计难题 [题目类型]Trie &题解: Trie的模板题,只不过这题坑点在没给数据范围,改成5e5就可以过了,用的刘汝佳蓝书模板 &代码: #include & ...
- hdu 1251 统计难题 字典树第一题。
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- hdu 1251 统计难题(字典树)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Total Subm ...
- HDU 1251 统计难题 字典树大水题
今天刚看的字典树, 就RE了一发, 字典树原理还是很简单的, 唯一的问题就是不知道一维够不够用, 就开的贼大, 这真的是容易MLE的东西啊, 赶紧去学优化吧. HDU-1251 统计难题 这道题唯一的 ...
随机推荐
- Windows1
① 对Windows的设置一般在, 所有设置, 控制面板(control), 管理方式打开此电脑和此电脑上的选项 ② 关闭对账号安全的检验, 在控制面板中, 找到系统和安全, 再找到更改用户账号控制设 ...
- More than one fragment with the name [spring_web] was found. This is not legal ...
今天在搭建springweb应用环境的时候启动tomcat报错More than one fragment with the name [spring_web] was found. This is ...
- codevs 4888 零件分组
4888 零件分组 时间限制: 1 s 空间限制: 16000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description 现有一些棍状零件,每个零件都有 ...
- SPEC CPU 2006编译perl 出错:undefined reference to `pow'
问题来源: 编译spec2006时,出现如下错误: cc -L/home/yrtan/benchmark/2006/CPU2006v1.0.1/tools/output/lib -L/usr/loca ...
- 动态加载sd卡或者手机内置存储卡的so库
package com.wsc.utils; import android.content.Context; import com.wsc.common.Entrance; import com.ws ...
- 11gR2 Agent 简介
目的:本文会对oracle 11gR2 集群件(Grid Infrastructure,以下简称GI) 新特性 agent进行介绍,包括 agent的功能,常见的agent介绍,以及基本的诊断方法. ...
- FMDB的使用方法(附Demo)
http://www.jianshu.com/p/54e74ce87404 最近在项目中需要在多个页面对同样的数据进行相关操作,于是便用到了FMDB数据库操作,以下便是FMDB的一些简单的使用方法.附 ...
- 第011课_串口(UART)的使用
from: 第011课_串口(UART)的使用 第001节_辅线1_硬件知识_UART硬件介绍 1. 串口的硬件介绍 UART的全称是 Universal Asynchronous Receiver ...
- 2018.4.6 java交易记录系统
题目 ###1.交易明细文件内容如下例: 客户号 姓名 所述机构号 性别 帐号 发生时间 发生额 000001|刘德华|0000|1|4155990188888888|20060720200005|3 ...
- Python 求两个文本文件以行为单位的交集 并集 差集
Python 求两个文本文件以行为单位的交集 并集 差集,来代码: s1 = set(open('a.txt','r').readlines()) s2 = set(open('b.txt','r') ...