统计难题

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

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

坑坑:用G++在杭电oj上提交会一直内存超限~

 #include<iostream>
#include<vector>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <math.h>
#include<algorithm>
#define ll long long
#define eps 1e-8
using namespace std; struct nodes
{
int cnt;
struct nodes *next[];
nodes()
{
int i;
cnt = ;
for(i = ; i < ; i++)
next[i] = NULL;
}
} root,*temp; void inserts(char *word)
{
nodes *cur = &root;
while(*word )
{
int t = *word - 'a';
if(cur->next[t] == NULL)
{
temp = (nodes *)malloc(sizeof(nodes));
temp->cnt = ;
for(int i = ; i < ; i++)
temp->next[i] = NULL;
cur->next[t] = temp;
}
cur = cur->next[t];
cur->cnt++;
word++;
}
} void searchs(char *word)
{
nodes *cur = &root;
int ans = ;
while(*word && cur)
{
cur = cur->next[*word - 'a'];
if(cur)
ans = cur->cnt;
else
{
ans = ;
break;
}
word++;
}
printf("%d\n",ans);
} int main(void)
{
char bank[];
char ss[]; while(gets(bank) && bank[] )
{
inserts(bank);
}
while(scanf("%s",ss) != -)
{
searchs(ss);
}
return ;
}

新增省内存方法,STL中的map:

 #include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <map>
#include <algorithm>
#define N 500015
#define INF 1000000
#define ll long long
using namespace std; int main(void)
{
map<string,int>Q;
char temp[];
int l;
while(gets(temp) && temp[])
{
l = (int)strlen(temp);
for(int i = l; i > ; i--)
{
temp[i] = '\0';
Q[temp]++;
}
}
while(scanf("%s",temp) != -)
{
printf("%d\n",Q[temp]);
}
return ;
}

hdu 1251 统计难题(trie树入门)的更多相关文章

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

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

  2. HDU - 1251 统计难题(trie树)

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

  3. hdu 1251 统计难题 trie入门

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

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

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

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

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

  6. HDU 1251 统计难题(Trie)

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

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

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

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

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

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

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

随机推荐

  1. Struts2转换器

    为什么进行类型转换 在基于HTTP协议的Web应用中 客户端请求的所有内容都以文本编码方式传输到服务器端 服务器端的编程语言却有着丰富的数据类型 继承StrutsTypeConverter抽象类 继承 ...

  2. java基础之System类

    System类概述System 类包含一些有用的类字段和方法.它不能被实例化. 成员方法 public static void gc()运行垃圾回收器 public static void exit( ...

  3. js 属性的遍历

    引自:http://es6.ruanyifeng.com/#docs/object 属性的遍历 ES6 一共有5种方法可以遍历对象的属性. (1)for...in for...in循环遍历对象自身的和 ...

  4. Linux常用技巧

    1.解决不能中文显示 xshell 终端语言显示选择UTF-8 #yum groupinstall chinese-support 2.heredocument报错“unexpected end of ...

  5. 大批量数据导出excel

    有次面试时,老板问我大批量数据一次性导出会有什么问题 感谢度娘提供,感谢原博主提供 https://www.cnblogs.com/zou90512/p/3989450.html

  6. warning LNK4098: 默认库“MSVCRT”与其他库的使用冲突;请使用 /NODEFAULTLIB:library

    解决方法:是所有项目的这个"代码生成"属性设置保持一致. 项目——属性——配置属性——C/C++——代码生成:他有/MT,/MTd,/Md,/MDd四个选项,你必须让所有使用的库都 ...

  7. Python基本数据类型之字典,集合,range

     注: ange()是python的内置函数,用于创建整数的列表,可以生成递增或者递减的数列.ange也是python的内置函数,用于创造xrange对象用于迭代. 在python3中,range() ...

  8. create-react-app 创建项目失败

    创建失败后查阅相关资料,亲测删除 C:\Users\Administrator\AppData\Roaming\npm-cache\ 该文件夹下所有内容后成功.

  9. RN 开发工具及发布release版本

    2.1.开发工具推荐visual studio code https://code.visualstudio.com/docs/?dv=win 选择安装react native tool 就可以了 2 ...

  10. 深入浅出 Java Concurrency (14): 锁机制 part 9 读写锁 (ReentrantReadWriteLock) (2)[转]

    这一节主要是谈谈读写锁的实现. 上一节中提到,ReadWriteLock看起来有两个锁:readLock/writeLock.如果真的是两个锁的话,它们之间又是如何相互影响的呢? 事实上在Reentr ...