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记 ...
随机推荐
- 高性能服务器开发之C++定时器
高性能服务器开发之C++定时器 来源: https://www.cnblogs.com/junye/p/5836552.html 写这篇文章前搜了下网上类似的文章,有很多,所以笔者的这篇文章就不对定时 ...
- [Leetcode] remove element 删除元素
Given an array and a value, remove all instances of that value in place and return the new length. T ...
- POJ2774 Long Long Message 【后缀数组lcp】
长长的消息 时间限制: 4000MS 内存限制: 131072K 提交总数: 32393 接受: 13079 案件时间限制: 1000MS 描述 小猫在拜特兰的首府物理专业.最近有一个不幸的消 ...
- wget命令下载FTP整个目录进行文件备份
使用wget下载整个FTP目录,可以用于服务器间文件传输,进行远程备份.通过限制网速,可以解决带宽限制问题. #wget ftp://IP:PORT/* --ftp-user=xxx --ftp-pa ...
- 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 ...
- html中音频和视频
HTML5音频中的新元素标签 src:音频文件路径. autobuffer:设置是否在页面加载时自动缓冲音频. autoplay:设置音频是否自动播放. loop:设置音频是否要循环播放. contr ...
- import as from import 区别
在python中import或者from…import是用来导入相应的模块.那每一种有什么具体的差别呢? 一.import 只有import,为最简单的引入对应的包.例如: import ...
- 知问前端——日历UI(二)
datapicker外观选项 属性 默认值/类型 说明 disabled false/布尔值 禁用日历 numberOfMonths 1/数值 日历中同时显示的月份个数.默认为1,如果设置3就同时显示 ...
- 【POJ 1719】 Shooting Contest (二分图匹配)
题目链接 把每一列能射的两行和这一列连边,然后跑一边匈牙利就行了. #include <cstdio> #include <cstring> #include <algo ...
- 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 ...