http://acm.hdu.edu.cn/showproblem.php?pid=1251

统计难题

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

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

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

 
Output
对于每个提问,给出以该字符串为前缀的单词的数量.
 
Sample Input
banana
band
bee
absolute
acm
 
ba
b
band
abc
 
Sample Output
2
3
1
0
 
思路:字典树
代码:

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <map>
using namespace std; #define MAX 0x7fffffff struct node{
node* word[];
int n;
node(){
for(int i=;i<;i++) word[i]=NULL;
n=;
}
}*root; void Insert(char* s);
int Find(char* s); int main(){
//freopen("D:\\input.in","r",stdin);
//freopen("D:\\output.out","w",stdout);
char tmp[];
root=new node;
while(gets(tmp),strlen(tmp)){
Insert(tmp);
}
while(gets(tmp)!=NULL){
printf("%d\n",Find(tmp));
}
return ;
}
void Insert(char* s){
int len=strlen(s);
node *current=root,*new_node;
for(int i=;i<len;i++){
if(current->word[s[i]-'a']!=NULL){
current=current->word[s[i]-'a'];
current->n++;
}else{
new_node=new node;
current->word[s[i]-'a']=new_node;
current=current->word[s[i]-'a'];
}
}
}
int Find(char* s){
int len=strlen(s);
node *current=root;
for(int i=;i<len;i++){
if(current->word[s[i]-'a']!=NULL){
current=current->word[s[i]-'a'];
}else{ return ; }
}
return current->n;
}

hdoj1251-统计难题 【字典树】的更多相关文章

  1. hdoj1251 统计难题 字典树

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

  2. hdu 1251 统计难题 (字典树入门题)

    /******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...

  3. HDOJ/HDU 1251 统计难题(字典树啥的~Map水过)

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

  4. hdu_1251统计难题(字典树Trie)

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

  5. hdu 1251 统计难题 字典树第一题。

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

  6. hdu 1251 统计难题(字典树)

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others) Total Subm ...

  7. HDU-1251 统计难题,字典树或者map!

    统计难题 很久就看过这个题了,但不会~~~不会~~ 题意:给出一张单词表,然后下面有若干查询,每次给出一个单词,问单词表中是否存在以这个单词为前缀的单词,输出数量.本身也是自身的前缀.只有一组数据! ...

  8. HDU 1251 统计难题 字典树大水题

    今天刚看的字典树, 就RE了一发, 字典树原理还是很简单的, 唯一的问题就是不知道一维够不够用, 就开的贼大, 这真的是容易MLE的东西啊, 赶紧去学优化吧. HDU-1251 统计难题 这道题唯一的 ...

  9. hdu 1251 统计难题 (字典树(Trie)<PS:C++提交不得爆内存>)

    统计难题Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submis ...

  10. hdoj 1251 统计难题(字典树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 思路分析:该问题要求求出以某个字符串为前缀的单词数目,通过使用字典树,在字典树中添加count记 ...

随机推荐

  1. java学习——构造类之3!+5!=126

    package my_project; import java.util.Scanner; public class my_first_class { public static void main( ...

  2. ASP.NET Web Pages:发布网站

    ylbtech-.Net-ASP.NET Web Pages:发布网站 1.返回顶部 1. ASP.NET Web Pages - 发布网站 学习如何在不使用 WebMatrix 的情况下发布 Web ...

  3. [转]Java. SqlServer 使用

    public void add(Emp emp) throws Exception { String connectionUrl = "jdbc:sqlserver://localhost: ...

  4. phpexcel导入数据出现PHPExcel_RichText Object解决办法

    在导入excel的时候会出现异常情况,有的问题出现PHPExcel_RichText object,错误代码如下 PHPExcel_RichText Object ( [_richTextElemen ...

  5. JedisCluster模式尝试进行批量操作

    搭建完redis集群后,可以通过jedis的JedisCluster来访问Redis集群,这里列出使用jedisCluster的spring bean配置方式:   <bean id=" ...

  6. Oracle数据库的一些视图

    1.之前一直是SQL使用,没有深入学习数据库的底层知识,尤其使其进程以及锁以及底层的数据如何存储的这些知识. 2.渐渐的在开发中对数据库的要求慢慢高了 比如:临时表 -----BI里面,存储过程里面 ...

  7. 探究CSS中border-top属性的使用

    上一节我们介绍了CSS top属性的用法,那么这节关于CSS border-top属性用法学习起来就会轻松一些,border-top 简写属性把上边框的所有属性设置到一个声明中. 本文向大家描述一下C ...

  8. ElasticSearch 索引模块——全文检索

    curl -XPOST http://master:9200/djt/user/3/_update -d '{"doc":{"name":"我们是中国 ...

  9. cplex-Java-样例代码解析

    import ilog.cplex.IloCplex; import ilog.concert.*; /** * * * * 最大化 x1 + 2x2 + 3x3</br> * 约束 &l ...

  10. 网络性能测试工具iperf

    参考网站:https://www.cnblogs.com/yingsong/p/5682080.html https://docs.azure.cn/zh-cn/articles/azure-oper ...