统计难题

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

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 <cstring>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
#define lson(x) ((x<<1))
#define rson(x) ((x<<1)+1)
using namespace std;
typedef long long ll;
const int N=;
const int M=1e6+;
typedef struct TrieNode {
int nCount;
struct TrieNode *next[N];
} TrieNode;
TrieNode Memory[M];
int allocp=;
void InitTrieRoot(TrieNode **pRoot) {
*pRoot=NULL;
}
TrieNode *CreateTrieNode() {
TrieNode *p;
p=&Memory[allocp++];
p->nCount=;
for(int i=; i<N; i++) {
p->next[i]=NULL;
}
return p;
}
void InsertTrie(TrieNode **pRoot,char *s) {
int i,k;
TrieNode *p;
if(!(p=*pRoot))
p=*pRoot=CreateTrieNode();
i=;
while(s[i]) {
k=s[i++]-'a';
if(p->next[k]) p->next[k]->nCount++;
else p->next[k]=CreateTrieNode();
p=p->next[k];
}
}
int SearchTrie(TrieNode **pRoot,char *s) {
TrieNode *p;
int i,k;
if(!(p=*pRoot))
return ;
i=;
while(s[i]) {
k=s[i++]-'a';
if(p->next[k]==NULL) return ;
p=p->next[k];
}
return p->nCount;
}
int main() {
char str[];
TrieNode *Root=NULL;
InitTrieRoot(&Root);
while(gets(str)&&str[]) {
InsertTrie(&Root,str);
}
while(gets(str)) {
printf("%d\n",SearchTrie(&Root,str));
}
return ;
}

HDU 1251 统计难题 (字典树)(查询是否为前缀)的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

  7. HDU 1251 统计难题(字典树)

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

  8. HDU 1251统计难题 字典树

    字典树的应用. 数据结构第一次课的作业竟然就需要用到树了!!!这不科学啊.赶紧来熟悉一下字典树. 空间开销太大T T #include<cstdio> #include<cstrin ...

  9. hdu -1251 统计难题(字典树水题)

    http://acm.hdu.edu.cn/showproblem.php?pid=1251 建树之后 查询即可. G++提交 ME不知道为什么,c++就对了. #include <iostre ...

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

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

随机推荐

  1. 【MySQL】数据库 --MySQL的安装

    本篇教程主要讲解在CentOS 6.5下编译安装MySQL 5.6.14! 1.卸载旧版本: 使用下面的命令检测是否安装有MySQL server <span style="font- ...

  2. [Leetcode] Anagrams 颠倒字母构成词

    Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...

  3. [COGS 622] [NOIP2011] 玛雅游戏 模拟

    整个模拟的关键除了打出来就是一个剪枝:对于两个左右相邻的块你不用再走←,因为走→是等效的 #include<cstdio> #include<cstring> #include ...

  4. MySQL使用笔记(二)数据库基本操作

    By francis_hao    Dec 11,2016 数据库是什么 数据库是什么呢?对于MySQL来说,数据库是存储数据库对象的容器,参考[1]中的简单解释是:数据库是一个拥有特定排放顺序的文件 ...

  5. xmlns:sys="clr-namespace:System;assembly=mscorlib" NOTE: System;与assembly中间不能有空格

    xmlns:sys="clr-namespace:System;assembly=mscorlib"  NOTE: System;与assembly中间不能有空格 否则报错, Er ...

  6. 公共css

    * { margin: 0; padding: 0; word-break: break-all; font-family: Microsoft YaHei, tahoma, arial, Hirag ...

  7. Spring学习--通过注解配置 Bean (三)

    组件装配: <context:component-sacan> 元素还会自动注册 AutowiredAnnotationBeanPostProcesser 实例 , 该实例可以自动装配具有 ...

  8. 解决设置了background-size: cover; 但是图片在ios下显示不完整的问题

    设置 background-size: % 99.9%:

  9. Jquery Ajax异步上传

    <script> $(function(){ $('#filephoto').change(function(imgFile){ console.log(imgFile) var file ...

  10. spring boot修改内置容器tomcat的服务端口

    方式一 在spring boot的web 工程中,可以使用内置的web container.有时需要修改服务端口,可以通过配置类和@Configuration注解来完成. // MyConfigura ...