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最近很空,他想了一件没有 ...
随机推荐
- 带SoapHeader验证的WebServices
一般在项目中,制作的都是基于SOAP协议的webservices,其描述语言是WSDL.但是有时候在项目中,需要保证webservices的安全,需要对其进行进行验证,那么我们就要实现SoapHead ...
- 八 Django框架,模板语言
模板语言就是可以将动态数据在html模板渲染的语言 一.接收值渲染 locals()函数,写在请求响应render()函数里,可以将逻辑处理函数里的变量传到html用模板语言渲染 {{...}}接收一 ...
- SmartGit(试用期30后),个人继续使用的方法。
在我们做项目的过程中,我们会用到SmartGit这个软件来将本地的MAVEN项目push到国内的码云(https://git.oschina.net)或者是国外的github网站进行项目的管理,这个时 ...
- ONVIF测试方法及工具
设备是否支持ONVIF验证1 ONVIF Test Tool安装1.1PC安装环境要求:装有Microsoft .Net Framework 3.5或以上版本.1.2安装源文件请见:ONVIF Con ...
- 三种 Failover 之 Client-Side Connect time Failover、Client-Side TAF、Service-Side TAF
三种 Failover 之 Client-Side Connect time Failover.Client-Side TAF.Service-Side TAF 理论背景 Oracle RAC 同时 ...
- 找工作——JVM内存管理
1. JVM类加载机制 类从被加载到虚拟机内存开始,到卸载出内存为止,它的整个生命周期包括:加载.连接(验证.准备.解析).初始化.使用和卸载阶段. 加载:根据查找路径找到对应的class文件,然后倒 ...
- influxdb api
https://influxdb-python.readthedocs.io/en/latest/examples.html
- Python函数式编程(把函数作为参数传入)
map:接受两个参数(函数,Iterable),map将传入的函数依次作用于Iterable的每个元素,并且返回新的Iterable def f(x): return x*x r = map(f,[1 ...
- nodejs利用windows API读取文件属性(dll)
nodejs调用delphi编写的dll中,使用了dll调用windows api转读取文件属性,感觉使用nodejs也可直接调用windows api. 此处需用到windows系统的version ...
- numpy.matlib.randn(标准正态分布)
#网址 http://docs.scipy.org/doc/numpy/reference/generated/numpy.matlib.randn.html#numpy.matlib.randn n ...