题目链接: 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. C++(八)— 死锁原因及解决方法

    1.死锁原因 死锁问题被认为是线程/进程间切换消耗系统性能的一种极端情况.在死锁时,线程/进程间相互等待资源,而又不释放自身的资源,导致无穷无尽的等待,其结果是任务永远无法执行完成. 打个比方,假设有 ...

  2. 十九 Django框架,发送邮件

    全局配置settings.py EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' #发送邮件引擎 EMAIL_USE_TLS ...

  3. Tomcat翻译--Tomcat Web Application Deployment(Tomcat中部署web应用)

    原文:http://tomcat.apache.org/tomcat-7.0-doc/deployer-howto.html Introduction(介绍) Deployment is the te ...

  4. Convolutional Neural Networks for Visual Recognition 6

    -###Learning 前面,我们介绍了神经网络的构成,数据的预处理,权值的初始化等等.这一讲,我们将要介绍神经网络参数学习的过程. Gradient Checks 梯度的运算,在理论上是简单的,但 ...

  5. bzoj1000~1025

    以后还是这样 25道题一起发 看着爽 noip失利之后发粪涂墙 刷了一波bzoj 题解: bzoj1000 A+B问题 这题不同的人有不同的写法,我写了个线段树套Treap,应该还是挺简单的 但是看别 ...

  6. Gym - 100513B:Colored Blankets (构造)(存疑)

    题意:给定N的棒棒,K种颜色,每个棒棒的两端可以涂色.现在已知所有的线段要么有一端涂色,要么两端都没有涂色,现在要求把所有的没涂色的部分涂色,使得我们可以把涂色后的棒棒分为N/K组,每组的涂色情况相同 ...

  7. 【LeetCode】019. Remove Nth Node From End of List

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  8. 洛谷P4721 【模板】分治 FFT(生成函数+多项式求逆)

    传送门 我是用多项式求逆做的因为分治FFT看不懂…… upd:分治FFT的看这里 话说这个万恶的生成函数到底是什么东西…… 我们令$F(x)=\sum_{i=0}^\infty f_ix^i,G(x) ...

  9. poj 1273 Drainage Ditches(最大流,E-K算法)

    一.Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clove ...

  10. 【转】 Pro Android学习笔记(四九):ActionBar(2):Action图标区

    目录(?)[-] ActionBar的隐藏和现实 ActionBar的action图标区 ActionBar的隐藏和现实 ActionBar bar = getActionBar();bar.hide ...