题目链接: 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. 十七 Django框架,文件上传

    1.自定义上传[推荐] 请求对象.FILES.get()获取上传文件的对象上传对象.name获取上传文件名称上传对象.chunks()获取上传数据包,字节码类型 html <!DOCTYPE h ...

  2. MessFormat的简单使用

    MessageFormat用法java.text.MessageFormat 作用:MessageFormat 获取一组对象,格式化这些对象,然后将格式化后的字符串插入到模式中的适当位置. Messa ...

  3. mysql字符串的隐式转换导致查询异常

    如果mysql某个字段(name)类型为varchar, 加了索引,在执行where查询的时候,传入了int的值,这样就会全表扫描,把每一条的值都转换成int(会出现"中国"-&g ...

  4. 关于解决SSHD 连接 认证失败的问题

    网上找有很多方法,有时候情况不一样 ,也不实用 其实找到解决问题的思路更总要 首先分析日志文件 less /var/log/secure | grep sshd ,看具体出现什么问题 然后再去搜索相关 ...

  5. 2017-2018-1 20179215《Linux内核原理与分析》第二周作业

    20179215<Linux内核原理与分析>第二周作业 这一周主要了解了计算机是如何工作的,包括现在存储程序计算机的工作模型.X86汇编指令包括几种内存地址的寻址方式和push.pop.c ...

  6. nodejs 上传图片(服务端输出全部代码)

    下面代码,全部都是nodejs端的,不用客户端代码.也就是,选择图片的form表单以及上传完毕预览图片的html,都是由node服务端输出的. 1 启动代码:(node upload.js) var ...

  7. Marionettejs

    Marionette是牵线木偶的意思,这个库是对Backbone的一次更高层次封装.这样的封装有两个目标: 减少重复的工作,提高使用Backbonejs时的生产效率给复杂应用页面提供更多的结构,以支撑 ...

  8. 如何使用ODB(How to use odb On windows)

    1.下载ODB library:ODB Compiler,Common Runtime Library,Database Runtime Library. http://www.codesynthes ...

  9. Poj 2328 Guessing Game(猜数字游戏)

    一.题目大意 两个小盆友玩猜数字游戏,一个小盆友心里想着1~10中的一个数字,另一个小盆友猜.如果猜的数字比实际的大,则告诉他"too high",小则"too low& ...

  10. Poj 1936,3302 Subsequence(LCS)

    一.Description(3302) Given a string s of length n, a subsequence of it, is defined as another string ...