统计难题

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. 高性能服务器开发之C++定时器

    高性能服务器开发之C++定时器 来源: https://www.cnblogs.com/junye/p/5836552.html 写这篇文章前搜了下网上类似的文章,有很多,所以笔者的这篇文章就不对定时 ...

  2. [Leetcode] remove element 删除元素

    Given an array and a value, remove all instances of that value in place and return the new length. T ...

  3. POJ2774 Long Long Message 【后缀数组lcp】

    长长的消息 时间限制: 4000MS   内存限制: 131072K 提交总数: 32393   接受: 13079 案件时间限制: 1000MS 描述 小猫在拜特兰的首府物理专业.最近有一个不幸的消 ...

  4. wget命令下载FTP整个目录进行文件备份

    使用wget下载整个FTP目录,可以用于服务器间文件传输,进行远程备份.通过限制网速,可以解决带宽限制问题. #wget ftp://IP:PORT/* --ftp-user=xxx --ftp-pa ...

  5. How do I use EC2 Systems Manager to join an instance to my AWS Directory Service domain?

    1. Create new role "EC2RoleforSSM" in AWS IAM AWS->IAM->Roles->Create role->Se ...

  6. html中音频和视频

    HTML5音频中的新元素标签 src:音频文件路径. autobuffer:设置是否在页面加载时自动缓冲音频. autoplay:设置音频是否自动播放. loop:设置音频是否要循环播放. contr ...

  7. import as from import 区别

    在python中import或者from…import是用来导入相应的模块.那每一种有什么具体的差别呢? 一.import        只有import,为最简单的引入对应的包.例如: import ...

  8. 知问前端——日历UI(二)

    datapicker外观选项 属性 默认值/类型 说明 disabled false/布尔值 禁用日历 numberOfMonths 1/数值 日历中同时显示的月份个数.默认为1,如果设置3就同时显示 ...

  9. 【POJ 1719】 Shooting Contest (二分图匹配)

    题目链接 把每一列能射的两行和这一列连边,然后跑一边匈牙利就行了. #include <cstdio> #include <cstring> #include <algo ...

  10. Local Authentication Using Challenge Response with Yubikey for CentOS 7

    Connect Yubikey  ,then initialize YubiKey slot 2: ykpersonalize -2 -ochal-resp -ochal-hmac -ohmac-lt ...