统计难题

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. IFROG线上赛做过的题目

    #6 1068: 找规律 int main(){ int t,n; cin>>t; while(t--){ cin>>n; ==)printf(); else printf(& ...

  2. axios超时重发

    axios的超时是在response中处理的,所以要在response中添加拦截器: axios.interceptors.response.use(undefined, function axios ...

  3. 如何抓取开了gzip的网页

    有时候用 file_get_contents() 函数抓取网页会发生乱码现象.有两个原因会导致乱码,一个是编码问题,一个是目标页面开了Gzip. 编码问题好办,把抓取到的内容转下编码即可($conte ...

  4. poj 2378 Tree Cutting 树形dp

    After Farmer John realized that Bessie had installed a "tree-shaped" network among his N ( ...

  5. 怎么把centos虚拟机zip文件导入vm虚拟机中

    执行以上三步就可以将一个压缩的centoszip文件导入到虚拟机中

  6. 两数之和 [ leetcode ]

    原题地址:https://leetcode-cn.com/articles/two-sum/ 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元 ...

  7. Android 实现对图片 Exif 的修改(Android 自带的方法)

    很多时候我们都要对我们的图片信息进行一些处理,比如向图片中写入经纬度,拍摄时间,设备信息,作者等等. 这个时候我们就要对我们的图片Exif进行写入信息的操作,当然,我们想知道图片的Exif信息,也可以 ...

  8. 百练2505:A multiplication game

    传送门:http://bailian.openjudge.cn/practice/2505/ [题解] 我们找找规律: 1~9显然是Stan wins. 10~18是Ollie wins. 19~16 ...

  9. 填坑webpack

    1.Concepts: webpack is a module bundler for modern JS applications. Since there are lots of complex ...

  10. Apache服务器

    Apache服务器 一  简介 1   www:world  wide  web    万维网 http    协议:  超文本传输协议 HTML语言:  超文本标识语言 2   URL:统一资源定位 ...