统计难题

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)
Total Submission(s): 41046    Accepted Submission(s):
14830

Problem Description
Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).
 
Input
输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串.

注意:本题只有一组测试数据,处理到文件结束.

 
Output
对于每个提问,给出以该字符串为前缀的单词的数量.
 
Sample Input
banana
band
bee
absolute
acm

ba
b
band
abc

 
Sample Output
2
3
1
0
 
Author
Ignatius.L
 
 #include<cstdio>
#include<cstring>
const int MAXN = ; struct Trie
{
int ch[MAXN][];
int val[MAXN];
int size;//节点总数
void Clear()
{
size = ;
memset(ch[],,sizeof(ch[]));
memset(val,,sizeof(val));
}
int idx(char c)
{
return c - 'a'; //字符c的编号
}
void Insert(const char*s)
{
int u = , len = strlen(s);
for (int i=; i<len; ++i)
{
int c = idx(s[i]);
if (!ch[u][c])
{
memset(ch[size],,sizeof(ch[size]));
ch[u][c] = size++;
}
u = ch[u][c];
val[u]++;
}
}
int Find(const char* s)
{
int u = , len = strlen(s);
for (int i=; i<len; ++i)
{
int c = idx(s[i]);
if (!ch[u][c]) return ;
u = ch[u][c];
}
return val[u] ;
}
}t; int main()
{
char word[];
t.Clear();
while (gets(word)&&strcmp(word,""))
{
t.Insert(word);
}
while (scanf("%s",word)!=EOF)
printf("%d\n",t.Find(word));
return ;
}

hdu1251统计难题(trie)的更多相关文章

  1. HDU1251 统计难题 Trie树

    题目很水,但毕竟是自己第一道的Trie,所以还是发一下吧.Trie的更多的应用慢慢学,AC自动机什么的也慢慢学.... #include<iostream> #include<cst ...

  2. [hdu1251]统计难题(trie模板题)

    题意:返回字典中所有以测试串为前缀的字符串总数. 解题关键:trie模板题,由AC自动机的板子稍加改造而来. #include<cstdio> #include<cstring> ...

  3. HDU1251 统计难题 trie树 简单

    http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意: 找前缀数量 裸模板 #include<cstdio> #include<cstr ...

  4. HDU1251统计难题---Trie Tree

    map巧过 #include <stdio.h> #include <string.h> #include <map> #include <string> ...

  5. HDU1251 统计难题(Trie)

    统计难题 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 Subm ...

  7. hdu1251 统计难题

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

  8. hdu 1251 统计难题 trie入门

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

  9. HDU 1251 统计难题(Trie)

    统计难题 [题目链接]统计难题 [题目类型]Trie &题解: Trie的模板题,只不过这题坑点在没给数据范围,改成5e5就可以过了,用的刘汝佳蓝书模板 &代码: #include & ...

随机推荐

  1. vue-表单绑定

    表单数据绑定1.1你可以用 v-model 指令在表单控件元素上创建双向数据绑定.它会根据控件类型自动选取正确的方法来更新元素.尽管有些神奇,但 v-model 本质上不过是语法糖,它负责监听用户的输 ...

  2. Windows 静态IP脚本

    @echo off echo 快速设置IP地址和DNS为“静态” set 连接名称=以太网 set ip地址=192.168.1.80 set 子网掩码=255.255.255.0 set 网关地址= ...

  3. react及flux架构范例Todomvc分析

    react及flux架构范例Todomvc分析 通过分析flux-todomvc源码,学习如何通过react构建web程序,了解编写react应用程序的一般步骤,同时掌握Flux的单向数据流动架构思想 ...

  4. DOM(三):querySelector和querySelectorAll

    querySelector()方法querySelector()方法接收一个css选择符,返回与该模式匹配的第一个元素,如果没有找到匹配的元素,返回null. //取得body元素 var body ...

  5. 【转】Android 组件系列-----Activity保存状态

    本篇随笔将详细的讲解Activity保存状态的概念,也就是saving activity state. 一.Activity状态保持概念 保存Activity的状态是非常重要的,例如我们在玩一个游戏的 ...

  6. python遇到IndentationError: unexpected indent

    由于tab和空格混用而导致的问题,解决办法如下: 在SubLime中View中的Identation中的Convert Indentitation to Spaces即可

  7. 【洛谷P1063】NOIP2006能量项链

    能量项链 https://www.luogu.org/problemnew/show/P1063 好像比合并石子更水.. 区间动规,f[l][r]表示合并区间l~r的最大能量 按区间长度dp 枚举中间 ...

  8. 继承FileInputFormat类来理解 FileInputFormat类

    import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.had ...

  9. GPGPU::数学基础教程

    并行方向需要学习的数学知识 http://dev.gameres.com/Program/Visual/3D/GPGPU_math_Tutorial.html

  10. 前端HTML基础

    1.0开发工具介绍 sublime的使用技巧链接 HTML特殊符号表 1.1 html概念 超文本标记语言(Hypertext Markup Language),属于一种描述性的标记语言(markup ...