3道入门字典树例题,以及模板【HDU1251/HDU1305/HDU1671】
HDU1251:http://acm.hdu.edu.cn/showproblem.php?pid=1251
题目大意:求得以该字符串为前缀的数目,注意输入格式就行了。
#include<stdio.h>
#include<string.h> char str[];
int trie[ * ][], cnt;
int sum[ * ]; void insert()
{
int len = strlen(str);
int rt = ;
for(int i = ; i < len; i ++)
{
int id = str[i] - 'a';
if(!trie[rt][id])
trie[rt][id] = ++ cnt;
rt = trie[rt][id];
sum[rt] ++;
}
} int serach()
{
int len = strlen(str);
int rt = ;
for(int i = ; i < len; i ++)
{
int id = str[i] - 'a';
if(!trie[rt][id])
return ;
rt = trie[rt][id];
}
return sum[rt];
} int main()
{
while(gets(str))
{
if(strlen(str) == )
break;
insert();
}
while(scanf("%s", str) != EOF)
{
printf("%d\n", serach());
}
return ;
}
HDU1251
HDU1305:http://acm.hdu.edu.cn/showproblem.php?pid=1305
题目大意:字符串之间互相不为前缀,则输出YES 否则NO
#include<stdio.h>
#include<string.h>
#define mem(a, b) memset(a, b, sizeof(a)) int trie[][], tot, k_rt[], cnt, sum[];
char str[]; void insert()
{
int len = strlen(str);
int root = ;
for(int i = ; i < len; i ++)
{
int id = str[i] - '';
if(!trie[root][id])
trie[root][id] = ++ tot;
root = trie[root][id];
sum[root] ++;
}
k_rt[++ cnt] = root; //记录每个01字符串结束的位置
} void init()
{
mem(trie, );
mem(k_rt, );
mem(sum, );
cnt = , tot = ;
} int main()
{
int k = , flag;
while(scanf("%s", str) != EOF)
{
if(str[] != '')
insert();
else
{
flag = ;
for(int i = ; i <= cnt; i ++)
{
if(sum[k_rt[i]] >= )
{
flag = ;
break;
}
}
if(flag)
printf("Set %d is not immediately decodable\n", k ++);
else
printf("Set %d is immediately decodable\n", k ++);
init();
}
}
return ;
}
HDU1305
HDU1671:http://acm.hdu.edu.cn/showproblem.php?pid=1671
题目大意:与HDU1251是一样的
#include<stdio.h>
#include<string.h>
#define mem(a, b) memset(a, b, sizeof(a)) char str[];
int trie[][], tot, cnt, k_rt[], sum[]; void insert()
{
int len = strlen(str);
int root = ;
for(int i = ; i < len; i ++)
{
int id = str[i] - '';
if(!trie[root][id])
trie[root][id] = ++ tot;
root = trie[root][id];
sum[root] ++;
}
k_rt[++ cnt] = root; //记录每个字符串的结束位置 ,通过结束位置上的出现次数判断是否是其他字符串的前缀
} void init()
{
mem(trie, );
mem(sum, );
mem(k_rt, );
tot = , cnt = ;
} int main()
{
int T, n, flag;
scanf("%d", &T);
while(T --)
{
init();
flag = ;
scanf("%d", &n);
getchar();
for(int i = ; i <= n; i ++)
{
scanf("%s", str);
insert();
}
for(int i = ; i <= cnt; i ++)
{
if(sum[k_rt[i]] >= )
{
flag = ;
break;
}
}
if(flag)
printf("NO\n");
else
printf("YES\n");
}
return ;
}
HDU1671
模板
字典树建树模板:
//建trie树
void insert()
{
int len = strlen(str);
int root = ;
for(int i = ; i < len; i ++)
{
int id = str[i] - '';
if(!trie[root][id])
trie[root][id] = ++ tot;
root = trie[root][id];
sum[root] ++; //记录数目则需要
}
pos[++ cnt] = root; //记录每个字符串的结束位置 ,通
// 过结束位置上的出现次数判断是否是其他字符串的前缀
} // 若根据输入判断,则不需要这句话,查找输入的字符串最后一个位置的出现次数即可
建trie树
字典树查找模板:
//trie树查找
int serach()//bool serach() , 该字符串为前缀的数目/是否存在以该字符串为前缀的字符串
{
int len = strlen(str);
int rt = ;
for(int i = ; i < len; i ++)
{
int id = str[i] - 'a';
if(!trie[rt][id])
return ;//return false;
rt = trie[rt][id];
}//达到字符串最后一个字符所在位置
return sum[rt]; // return true;
}
字典树查找
3道入门字典树例题,以及模板【HDU1251/HDU1305/HDU1671】的更多相关文章
- 字典树 && 例题 Xor Sum HDU - 4825 (板子)
一.字典树描述:Trie树,即字典树,又称单词查找树或键树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它的优 ...
- P1184 高手之在一起(字典树模板题,hash算法, map)
哎,唯一值得说明的是,这道题的输入有bug 先把字典树的算法模板放一下 #include<iostream> #include<cstring> using namespace ...
- HDU 1251 统计难题(字典树入门模板题 很重要)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- Tire树总结(模板+例题)
题目来自<算法竞赛设计指南> Tire树是一种可以快速查找字符串的数据结构 模板 #include<cstdio> #include<algorithm> #inc ...
- hdu1251+字典树常用模板
这里只简单给出几个常用的字典树的模板,要看具体介绍的请看:传送门 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现) ...
- HDU 4825 Xor Sum (模板题)【01字典树】
<题目链接> 题目大意: 给定n个数,进行m次查找,每次查找输出n个数中与给定数异或结果最大的数. 解题分析: 01字典树模板题,01字典树在求解异或问题上十分高效.利用给定数据的二进制数 ...
- 字典树模板题(统计难题 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 统计难题(字典树模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意:给出一些单词,然后有多次询问,每次输出以该单词为前缀的单词的数量. 思路: 字典树入门题. #inc ...
随机推荐
- 【VS调试】VS调试的时候使用IP地址,局域网的设备可以访问并调试
使用IIS Express调试,只能通过 http://localhost:端口 进行访问 客户端的设备如何才能通过 http://ip地址:端口 访问后台程序进行调试呢? 第一步,打开项目属性, ...
- docker 共享卷
宿主机与容器共享存储 [root@docker02 ~]# docker run -it -v /var/webroot:/abc 192.168.1.21:5000/busybox # 宿主机文件 ...
- POI读取格式化后的单元格数据
public static String getFormattedValue(Cell cell) { FormulaEvaluator evaluator = cell.getSheet().get ...
- C++自定义方法类、调用
如求矩形.圆的面积的方法,可以写成一个area类 area.h中只声明,不实现: #ifndef AREA_H //防止重复引用 #define AREA_H class area{//自定义类 pu ...
- BZOJ 3931: [CQOI2015]网络吞吐量 Dijkstra+最大流
这个没啥难的. 只保留可以转移最短路的边,然后拆点跑一个最大流即可. #include <bits/stdc++.h> #define N 1004 #define M 250004 #d ...
- Mac下Tomcat安装&配置&80默认端口设置
序言: 在学习Tomcat时, 部署虚拟服务主机时,遇到了无响应的情况.原以为是应为Tomcat默认端口8080在调整至(进行端口转发设置)默认端口80会和Mac自带Apache起冲突.但是也有同学使 ...
- NetCore2.0无法下载apk文件 IIS设置
把apk 文件放到网站的wwwroot目录 1.IIS设置MIME类型添加.apk, MIME类型:application/vnd.android.package-archive 2.然后StarUp ...
- [Java]用 MessageFormat 拼接组合字符串
package com.hy; import java.text.MessageFormat; public class Test3 { public static void main(String[ ...
- WordPress窗体化侧边栏
窗体化侧边栏是一个支持 Widget 的侧边栏或者说是窗体化(widgetized)的侧边栏几乎是 WordPress 主题的标准. 首先,什么是窗体化(widgetizing)呢?简单的说,窗体化就 ...
- @Transactional(事务讲解)和springboot 整合事务
概述 事务在编程中分为两种:声明式事务处理和编程式事务处理 编程式事务处理:编码方式实现事务管理,常与模版类TransactionTemplate(推荐使用) 在业务代码中实现事务. 可知编程式事务每 ...