题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1251

题意: 中文题诶~

思路: 字典树模板

代码1: 动态内存, 比较好理解一点, 不过速度略慢, 代码略长

 #include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std; const int MAXN = ;
const int MAX = ;
char str[MAX]; struct node{
int count;
node *next[MAXN];
node(){
count = ;
for(int i = ; i < MAXN; i++){
next[i] = NULL;
}
}
}; void insert(node *p, char *str){
for(int i = ; str[i] != '\0'; i++){
int cnt = str[i] - 'a';
if(p -> next[cnt] == NULL) p -> next[cnt] = new node();
p = p -> next[cnt];
p -> count++;
}
} int query(node *p, char *str){
for(int i = ; str[i] != '\0'; i++){
int cnt = str[i] - 'a';
p = p -> next[cnt];
if(!p) return ;
}
return p -> count;
} void Free(node *p){
if(!p) return;
for(int i = ; i < MAXN; i++){
if(p -> next[i]) Free(p -> next[i]);
}
free(p);
} int main(void){
node *root = new node();
while(gets(str) && str[] != '\0'){
insert(root, str);
}
while(gets(str)){
printf("%d\n", query(root, str));
}
Free(root);//本题为单组输入,不释放空间也没影响
return ;
}

代码2: 用数组模拟, 相对代码1略微难理解一点

 #include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std; const int MAXN = 1e5 + ;
int trie[MAXN << ][], sum[MAXN << ], num = ;
char str[];
//每个num对应一个节点,sum[i]为i出现的次数,trie[node][cnt]存储node这条链cnt节点后一个节点的位置,并标记当前节点是否存在 void insert(void){
int node = , indx = ;
while(str[indx]){
int cnt = str[indx++] - 'a';
if(!trie[node][cnt]) trie[node][cnt] = num++;
sum[trie[node][cnt]]++;
node = trie[node][cnt];
}
} int query(void){
int node = , indx = ;
while(str[indx]){
int cnt = str[indx++] - 'a';
if(!trie[node][cnt]) return ;
node = trie[node][cnt];
}
return sum[node];
} int main(void){
while(gets(str) && str[] != '\0'){
insert();
}
while(gets(str)){
printf("%d\n", query());
}
return ;
}

hdu1521(字典树模板)的更多相关文章

  1. 字典树模板题(统计难题 HDU - 1251)

    https://vjudge.net/problem/HDU-1251 标准的字典树模板题: 也注意一下输入方法: #include<iostream> #include<cstdi ...

  2. CH 1601 - 前缀统计 - [字典树模板题]

    题目链接:传送门 描述给定 $N$ 个字符串 $S_1,S_2,\cdots,S_N$,接下来进行 $M$ 次询问,每次询问给定一个字符串 $T$,求 $S_1 \sim S_N$ 中有多少个字符串是 ...

  3. HDU - 1251 字典树模板题

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

  4. 字典树模板 HDU - 1251

    题意: 给一些单词,换行键后,查找以后输入的单词作为前缀的话们在之前出现过几次. 思路: 字典树模板----像查字典的顺序一样 #include<string> #include<s ...

  5. P1184 高手之在一起(字典树模板题,hash算法, map)

    哎,唯一值得说明的是,这道题的输入有bug 先把字典树的算法模板放一下 #include<iostream> #include<cstring> using namespace ...

  6. hdu 1671 Phone List 字典树模板

    Given a list of phone numbers, determine if it is consistent in the sense that no number is the pref ...

  7. 字典树模板( 指针版 && 数组版 )

    模板 :  #include<string.h> #include<stdio.h> #include<malloc.h> #include<iostream ...

  8. Xor Sum---hdu4825(01字典树模板)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4825 题意:有n个数m个查找,每个查找有一个数x, 从序列中找到一个数y,使得x异或y最大 ...

  9. HDU 2072 - 单词数 - [(有点小坑的)字典树模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2072 Problem Descriptionlily的好朋友xiaoou333最近很空,他想了一件没有 ...

随机推荐

  1. python glances来监控linux服务器CPU 内存 IO使用

    什么是 Glances? Glances 是一个由 Python 编写,使用 psutil 库来从系统抓取信息的基于 curses 开发的跨平台命令行系统监视工具. 通过 Glances,我们可以监视 ...

  2. 什么是 Sass 其与SCSS区别是什么?

    Sass 官网上是这样描述 Sass 的: Sass 是一门高于 CSS 的元语言,它能用来清晰地.结构化地描述文件样式,有着比普通 CSS 更加强大的功能. Sass 能够提供更简洁.更优雅的语法, ...

  3. ashx页面缓存

    当用户访问页面时,整个页面将会被服务器保存在内存中,这样就对页面进行了缓存.当用户再次访问该页,页面不会再次执行数据操作,页面首先会检查服务器中是否存在缓存,如果缓存存在,则直接从缓存中获取页面信息, ...

  4. codeforces 629D D. Babaei and Birthday Cake (线段树+dp)

    D. Babaei and Birthday Cake time limit per test 2 seconds memory limit per test 256 megabytes input ...

  5. STL stl_config.h

    stl_config.h . // Filename: stl_config.h . . // Comment By: 凝霜 . // E-mail: mdl2009@vip.qq.com . // ...

  6. HihoCoder1664 01间隔方阵([Offer收割]编程练习赛40)(DP)

    给定一个NxM的01矩阵,小Hi希望从中找到一个01间隔的子方阵,并且方阵的边长越大越好. 例如对于 0100100 1000101 0101010 1010101 0101010 在右下角有一个4x ...

  7. 洛谷 P1767 家族_NOI导刊2010普及(10)

    题目描述 在一个与世隔绝的岛屿上,有一个有趣的现象:同一个家族的人家总是相邻的(这里的相邻是指东南西北四个方向),不同的家族之间总会有河流或是山丘隔绝,但同一个家族的人不一定有相同姓氏.现在给你岛上的 ...

  8. C# Hashtable赋值给另一个Hashtable时

    c#中想将一个hashtable的元素全部给另一个hashbale时, 使用迭代一个一个元素赋值 如: ammus.Clear(); IDictionaryEnumerator ie = _temp. ...

  9. 洛谷【P3407】散步

    我对状态空间的理解:https://www.cnblogs.com/AKMer/p/9622590.html 题目传送门:https://www.luogu.org/problemnew/show/P ...

  10. 51nod 1250 排列与交换——dp

    题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1250 仔细思考dp. 第一问,考虑已知 i-1 个数有多少种方案. ...