统计难题

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

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

【思路】

Trie。

构建一棵字典树,并维护tot[u]表示以u为根的子数中叶子节点的数目。

 需要注意的是maxnode要开大一些。

【代码】

 #include<cstdio>
#include<cstring>
#include<iostream>
#define FOR(a,b,c) for(int a=(b);a<=(c);a++)
using namespace std; const int maxnode = ;
//输入的字符串可能很多
const int maxl = ;
const int sigmasize = ; struct Trie{
int ch[maxnode][sigmasize];
int tot[maxnode];
int sz; Trie() {
sz=;
memset(ch[],,sizeof(ch[]));
tot[]=;
}
int idx(char c) { return c-'a'; }
void insert(char* s) {
int n=strlen(s),u=;
for(int i=;i<n;i++) {
int c=idx(s[i]);
if(!ch[u][c]) {
ch[u][c]=sz;
memset(ch[sz],,sizeof(ch[sz]));
tot[sz]=;
sz++;
}
u=ch[u][c];
tot[u]++;
}
}
int find(char* s) {
int n=strlen(s),u=;
for(int i=;i<n;i++) {
int c=idx(s[i]);
if(!ch[u][c]) return ;
else u=ch[u][c];
}
return tot[u];
}
}trie; int n; int main() {
//freopen("cin.in","r",stdin);
//freopen("coutme.out","w",stdout);
char s[maxl];
while(gets(s) && s[]) {
trie.insert(s);
}
while(gets(s)) {
printf("%d\n",trie.find(s));
}
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 Submi ...

  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. JAAS - Document

    JAAS 参考文档: JAAS Reference Guide JAAS Authentication Tutorial JAAS Authorization Tutorial LoginModule ...

  2. EF6.0+Mysql的问题

    最近在项目中使用EF for Mysql的时候遇到一个问题 public OrderManage GetOrders(OrderSearchCriteria criteria) { using (va ...

  3. Dictionary的遍历和修改

    /// <summary>        /// 初始化一个Dic        /// </summary>        public static void mainTe ...

  4. Linux 关于解压

    1.*.tar 用 tar –xvf 解压 2.*.gz 用 gzip -d或者gunzip 解压 3.*.tar.gz和*.tgz 用 tar –xzf 解压 4.*.bz2 用 bzip2 -d或 ...

  5. 基于ProGuard-Maven-Plugin的自定义代码混淆插件

    介绍 大家可能都会碰到一些代码比较敏感的项目场景,这个时候代码被反编译看到就不好了,这个时候就需要代码混淆插件来对代码进行混淆了. 基于Maven的项目一般会去考虑使用proguard-maven-p ...

  6. Qt Creator编译问题

    有时候需要自己编译Qt Creator,需要注意的就是qmake版本的问题,比如我用4.8.1和4.8.6同样编译出来的Qt Creator在同样的qtconfig-qt4下所呈现的效果是不一样的. ...

  7. cocod2d-x 之 CCDirector、CCScene、CCSprite

    CCDirector是控制游戏流程的主要组件. typedef enum { /// sets a 2D projection (orthogonal projection)2D投机模式 kCCDir ...

  8. PHP-HTML重要知识点笔记

    1.用frameset.frame和iframe还实现多窗口 2.在图片上利用映射距离usemap来实现按钮跳转.------第8尾集 3.表单必须要有name和value,因为抓包的时候,可发现必须 ...

  9. php 获取域名的whois 信息

    首先先了解几个文件操作函数: fwrite() 函数写入文件(可安全用于二进制文件). fwrite() 把 string 的内容写入文件指针 file 处. 如果指定了 length,当写入了 le ...

  10. 如何自定义RecycleView item的间距

    引言 在以前使用ListView和GridView时,设置item之间的间距还是相对比较简单的,因为它们的基本属性里面Android已经定义好了,可以直接设置属性值即可.但Google为了通用性和灵活 ...