hdu1251+字典树常用模板
这里只简单给出几个常用的字典树的模板,要看具体介绍的请看:传送门
注意:本题只有一组测试数据,处理到文件结束.
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+5;
char ch[15];
int tri[maxn][30];
int sum[maxn];
void insert(char * ch,int & cnt){
int len=strlen(ch);
int id=0;
for(int i=0;i<len;i++){
int tem=ch[i]-'a';
if(tri[id][tem]==0){
tri[id][tem]=++cnt;
} id=tri[id][tem];sum[id]++;
}
return ;
}
int find(char * ch){
int len=strlen(ch);
int id=0;
for(int i=0;i<len;i++){
int tem=ch[i]-'a';
if(tri[id][tem]==0){
return 0;
}
id=tri[id][tem];
}
return sum[id];
}
int main(){
char ch1;
int len=0;
int cnt=0;
while(ch1=getchar()){
if(ch1!='\n')
ch[len++]=ch1;
if(ch1=='\n'){
if(len==0)break; ch[len]='\0';
// puts(ch);
insert(ch,cnt);
len=0;
}
}
while(scanf("%s",ch)!=EOF){
//cout<<ch<<endl;
printf("%d\n",find(ch));
}
return 0;
}
建字典树:
void insert(char * ch,int & cnt){
int len=strlen(ch);
int id=0;
for(int i=0;i<len;i++){
int tem=ch[i]-'a';
if(tri[id][tem]==0){
tri[id][tem]=++cnt;
}
id=tri[id][tem];
sum[id]++;//可以用来查询以某字符串为前缀的字符串的数量 如hdu1251
}
vis[id]=1;//可以用来查询某字符串是否存在
return ;
}
查询:
int find(char * ch){
int len=strlen(ch);
int id=0;
for(int i=0;i<len;i++){
int tem=ch[i]-'a';
if(tri[id][tem]==0){
return 0;
}
id=tri[id][tem];
}
// return vis[id];
return sum[id];//也可以返回vis【id】,即判断当前字符串是否存在,存在返回一,不存在返回零
}
hdu1251+字典树常用模板的更多相关文章
- hdu1251 字典树trie 模板题
//字典树模板题.题意:给一个库,每次查询,求以之为前缀的单词数量. #include<iostream> #include<string> #include<vecto ...
- *HDU1251 字典树
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- HDU 1251 统计难题(字典树入门模板题 很重要)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- hdu1251 字典树or map
一道字典树的题,不过看起来用map更为简单 传送门 题意: 给出一堆字符串构成一个字典,求字典里以某字符串为前缀的字符串有几个 思路: 输入字符串时把字符串的前缀全部存进map并标记次数 查询时直接输 ...
- hdu1251字典树递归算法
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Total Subm ...
- HDU1251 字典树板子题
题意:中文题,统计以某字符串作为前缀的字符串个数 刚学字典树,理解起来十分简单,就是维护一个多叉树,这里用的是链表版本,后面就用的是数组版本了,个人更喜欢数组版本,这里的链表版本就因为 莫名其妙的错误 ...
- HDu-1247 Hat’s Words,字典树裸模板!
Hat's Words 题意:给出一张单词表求有多少个单词是由单词表里的两个单词组成,可以重复!按字典序输出这些单词. 思路:先建一个字典树,然后枚举每个单词,把每个单词任意拆分两部分然后查找. 目测 ...
- 统计难题---hdu1251字典树模板
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1251 node *head=(node*)malloc(sizeof(node)); for(int ...
- 字典树(Tire)模板
#include<stdio.h> #include<string.h> #include<stdlib.h> struct node { node *ne[]; ...
随机推荐
- mybatis关于ORM的使用以及设计(二)[DaoInterface 转换 Mapper代理对象]
第一节中,分析了Mybatis的ORM框架的初始化,这篇来分析SQL执行过程中,对象->SQL是如何转换的 其中包含两种映射思想 ①DAO接口->Mapper实例 ②执行DAO的方法时,参 ...
- 关于mysql文件导入提示“Variable @OLD_CHARACTER_SET_CLIENT can't be set to the value of @@CHARACTER_SET_CLIENT”问题分析
今天用myssqldump导出数据,然后再导入另外mysql数据库时,提示Variable @OLD_CHARACTER_SET_CLIENT can't be set to the value of ...
- 使用Microsoft自带的小工具将可执行文件(.exe)注册为系统服务
首先,我们从Microsoft下载Windows Resource Kits,Download 下载完成后,运行rktools.exe进行安装. 安装完成后,我们打开安装目录,将其中的"in ...
- 通过SID查找历史执行的SQL语句
这次某系统发生严重的阻塞,但是去查顶级会话,发现已经没有该对应的sql_id了,于是我们只用通过 v$active_session_history 视图来寻找.下面是查找的过程: 1.查找顶层ASH历 ...
- arcgis10.2 sde配置
本机配置好oracle11g server 64位+client 32位.
- list之flex布局写法
list之flex布局写法 移动端实际场景中经常会遇到将header置顶,然后下面list需要滚动的情况,通常的做法会是将header使用fixed的方式固定到顶部,然后list主体相对于header ...
- docker 启动报错 WARNING: IPv4 forwarding is disabled. Networking will not work.
解决办法: # vi /etc/sysctl.conf 或者 # vi /usr/lib/sysctl.d/00-system.conf 添加如下代码: net.ipv4.ip_forward ...
- 关于Eclipse for Python
学习Python一段时间,一直用Python的IDE进行开发,过程蛮顺利,但是,基于Visual Studio的使用经验,就希望尝试一种更友好的,更方便管理项目的IDE,分别尝试了PyCharm和Ec ...
- Interlij idea
https://blog.csdn.net/qq_30180559/article/details/79146795 http://baijiahao.baidu.com/s?id=157736618 ...
- VS2017编译SNMP++步骤记录
1.下载地址:https://www.agentpp.com/download.html 三个都下 2.新建解决方案 TestSnmp 3.下载后解压到解决方案文件夹(注意,解压后的 snmp++的版 ...