hdu1521(字典树模板)
题目链接: 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(字典树模板)的更多相关文章
- 字典树模板题(统计难题 HDU - 1251)
https://vjudge.net/problem/HDU-1251 标准的字典树模板题: 也注意一下输入方法: #include<iostream> #include<cstdi ...
- CH 1601 - 前缀统计 - [字典树模板题]
题目链接:传送门 描述给定 $N$ 个字符串 $S_1,S_2,\cdots,S_N$,接下来进行 $M$ 次询问,每次询问给定一个字符串 $T$,求 $S_1 \sim S_N$ 中有多少个字符串是 ...
- HDU - 1251 字典树模板题
Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). Input输入数据的第一部 ...
- 字典树模板 HDU - 1251
题意: 给一些单词,换行键后,查找以后输入的单词作为前缀的话们在之前出现过几次. 思路: 字典树模板----像查字典的顺序一样 #include<string> #include<s ...
- P1184 高手之在一起(字典树模板题,hash算法, map)
哎,唯一值得说明的是,这道题的输入有bug 先把字典树的算法模板放一下 #include<iostream> #include<cstring> using namespace ...
- hdu 1671 Phone List 字典树模板
Given a list of phone numbers, determine if it is consistent in the sense that no number is the pref ...
- 字典树模板( 指针版 && 数组版 )
模板 : #include<string.h> #include<stdio.h> #include<malloc.h> #include<iostream ...
- Xor Sum---hdu4825(01字典树模板)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4825 题意:有n个数m个查找,每个查找有一个数x, 从序列中找到一个数y,使得x异或y最大 ...
- HDU 2072 - 单词数 - [(有点小坑的)字典树模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2072 Problem Descriptionlily的好朋友xiaoou333最近很空,他想了一件没有 ...
随机推荐
- java:类集回顾
1.类集设置的主要目的:动态的对象数组 2.类集中有以下几个接口: Collection:是存放单值的最大父接口 |- List接口:里面的内容是允许重复的 |- ArrayList, Vector, ...
- 分享知识-快乐自己:解决 Maven 无法下载 fastdfs-client-java 依赖。
因为fastdfs-client-java-1.27-SNAPSHOT.jar这个依赖包在maven中央仓库是没有的. 需要自己编译源码成jar本地安装到maven 的本地仓库,安装完以后就能正常引用 ...
- C++(十)— 字符串进行插入、替换、查找、删除操作、substr
1.C++中对字符串进行插入.替换.删除操作 #include<iostream> #include<algorithm> #include<stdio.h> # ...
- uva 111 History Grading(lcs)
题目描述 在信息科学中有一些是关于在某些条件限制下,找出一些计算的最大值. 以历史考试来说好了,学生被要求对一些历史事件根据其发生的年代顺序来排列.所有事件顺序都正确的学生无疑的可以得满分.但是那些没 ...
- pow,sqrt使用时需注意
使用时注意类型,可见两者皆不可以用int 1.pow 函数声明: double pow (double base , double exponent); float pow (float base , ...
- codeforces 652A A. Gabriel and Caterpillar(水题)
题目链接: A. Gabriel and Caterpillar time limit per test 1 second memory limit per test 256 megabytes in ...
- 【leetcode刷题笔记】ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- 使用UIVisualEffectView创建毛玻璃效果
UIVisuaEffectView :继承自UIView,可以看成是专门用于处理毛玻璃效果的视图,只要我们将这个特殊的View添加到其他视图(eg. ImageView )上面,被该UIVisuaEf ...
- 检测 iOS 的 APP 性能的一些方法
本文转载于微信公众号:iOS大全 首先如果遇到应用卡顿或者因为内存占用过多时一般使用Instruments里的来进行检测.但对于复杂情况可能就需要用到子线程监控主线程的方式来了,下面我对这些方法做些介 ...
- PLSQL Developer安装、配置、连接oracle数据库
0.资源准备 1) PLSQL Developer安装包(由于安装包超过10M,无法上传,请自行下载) 2) instantclient_11_2安装包(由于安装包超过10M,无法上传,请自行下载) ...