HDU 1251 统计难题 (字典树)(查询是否为前缀)
统计难题
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)
Total Submission(s): 37626 Accepted Submission(s): 13858
注意:本题只有一组测试数据,处理到文件结束.
band
bee
absolute
acm
ba
b
band
abc
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 统计难题 (字典树)(查询是否为前缀)的更多相关文章
- hdu 1251 统计难题 (字典树入门题)
/******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...
- HDOJ/HDU 1251 统计难题(字典树啥的~Map水过)
Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己 ...
- hdu 1251 统计难题(字典树)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Total Subm ...
- hdu 1251 统计难题 字典树第一题。
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- HDU 1251 统计难题 字典树大水题
今天刚看的字典树, 就RE了一发, 字典树原理还是很简单的, 唯一的问题就是不知道一维够不够用, 就开的贼大, 这真的是容易MLE的东西啊, 赶紧去学优化吧. HDU-1251 统计难题 这道题唯一的 ...
- hdu 1251 统计难题 (字典树(Trie)<PS:C++提交不得爆内存>)
统计难题Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submis ...
- HDU 1251 统计难题(字典树)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- HDU 1251统计难题 字典树
字典树的应用. 数据结构第一次课的作业竟然就需要用到树了!!!这不科学啊.赶紧来熟悉一下字典树. 空间开销太大T T #include<cstdio> #include<cstrin ...
- hdu -1251 统计难题(字典树水题)
http://acm.hdu.edu.cn/showproblem.php?pid=1251 建树之后 查询即可. G++提交 ME不知道为什么,c++就对了. #include <iostre ...
- hdoj 1251 统计难题(字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 思路分析:该问题要求求出以某个字符串为前缀的单词数目,通过使用字典树,在字典树中添加count记 ...
随机推荐
- 【MySQL】数据库 --MySQL的安装
本篇教程主要讲解在CentOS 6.5下编译安装MySQL 5.6.14! 1.卸载旧版本: 使用下面的命令检测是否安装有MySQL server <span style="font- ...
- [Leetcode] Anagrams 颠倒字母构成词
Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...
- [COGS 622] [NOIP2011] 玛雅游戏 模拟
整个模拟的关键除了打出来就是一个剪枝:对于两个左右相邻的块你不用再走←,因为走→是等效的 #include<cstdio> #include<cstring> #include ...
- MySQL使用笔记(二)数据库基本操作
By francis_hao Dec 11,2016 数据库是什么 数据库是什么呢?对于MySQL来说,数据库是存储数据库对象的容器,参考[1]中的简单解释是:数据库是一个拥有特定排放顺序的文件 ...
- xmlns:sys="clr-namespace:System;assembly=mscorlib" NOTE: System;与assembly中间不能有空格
xmlns:sys="clr-namespace:System;assembly=mscorlib" NOTE: System;与assembly中间不能有空格 否则报错, Er ...
- 公共css
* { margin: 0; padding: 0; word-break: break-all; font-family: Microsoft YaHei, tahoma, arial, Hirag ...
- Spring学习--通过注解配置 Bean (三)
组件装配: <context:component-sacan> 元素还会自动注册 AutowiredAnnotationBeanPostProcesser 实例 , 该实例可以自动装配具有 ...
- 解决设置了background-size: cover; 但是图片在ios下显示不完整的问题
设置 background-size: % 99.9%:
- Jquery Ajax异步上传
<script> $(function(){ $('#filephoto').change(function(imgFile){ console.log(imgFile) var file ...
- spring boot修改内置容器tomcat的服务端口
方式一 在spring boot的web 工程中,可以使用内置的web container.有时需要修改服务端口,可以通过配置类和@Configuration注解来完成. // MyConfigura ...