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】的更多相关文章

  1. 字典树 && 例题 Xor Sum HDU - 4825 (板子)

    一.字典树描述:Trie树,即字典树,又称单词查找树或键树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它的优 ...

  2. P1184 高手之在一起(字典树模板题,hash算法, map)

    哎,唯一值得说明的是,这道题的输入有bug 先把字典树的算法模板放一下 #include<iostream> #include<cstring> using namespace ...

  3. HDU 1251 统计难题(字典树入门模板题 很重要)

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

  4. Tire树总结(模板+例题)

    题目来自<算法竞赛设计指南> Tire树是一种可以快速查找字符串的数据结构 模板 #include<cstdio> #include<algorithm> #inc ...

  5. hdu1251+字典树常用模板

    这里只简单给出几个常用的字典树的模板,要看具体介绍的请看:传送门 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现) ...

  6. HDU 4825 Xor Sum (模板题)【01字典树】

    <题目链接> 题目大意: 给定n个数,进行m次查找,每次查找输出n个数中与给定数异或结果最大的数. 解题分析: 01字典树模板题,01字典树在求解异或问题上十分高效.利用给定数据的二进制数 ...

  7. 字典树模板题(统计难题 HDU - 1251)

    https://vjudge.net/problem/HDU-1251 标准的字典树模板题: 也注意一下输入方法: #include<iostream> #include<cstdi ...

  8. CH 1601 - 前缀统计 - [字典树模板题]

    题目链接:传送门 描述给定 $N$ 个字符串 $S_1,S_2,\cdots,S_N$,接下来进行 $M$ 次询问,每次询问给定一个字符串 $T$,求 $S_1 \sim S_N$ 中有多少个字符串是 ...

  9. HDU 1251 统计难题(字典树模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意:给出一些单词,然后有多次询问,每次输出以该单词为前缀的单词的数量. 思路: 字典树入门题. #inc ...

随机推荐

  1. windows 查看端口占用和杀死进程

    在windows命令行窗口下执行:C:\>netstat -aon|findstr "3306" 如上图,端口被进程号为5056的进程占用,继续执行下面命令:C:\>t ...

  2. nginx常用命令和配置

    1.常用命令 查看版本号: ./nginx -v   启动nginx:在/usr/local/nginx/sbin 目录下执行  ./nginx   关闭nginx:在/usr/local/nginx ...

  3. PHP mysqli_field_count() 函数

    定义和用法 mysqli_field_count() 函数返回最近查询的列数. 假设我们有一个 "websites" 表,其中有 5 个字段 20 行记录.返回最近查询的列数: & ...

  4. 【luoguP4124 】[CQOI2016]手机号码

    题目描述 人们选择手机号码时都希望号码好记.吉利.比如号码中含有几位相邻的相同数字.不含谐音不吉利的数字等.手机运营商在发行新号码时也会考虑这些因素,从号段中选取含有某些特征的号码单独出售.为了便于前 ...

  5. Mybatis源码学习之DataSource(七)_2

    接上节数据源,本节我们将继续学习未完成的部分,包括无连接池情况下的分析.为什么使用连接池.及mybatis连接池的具体管理原理 不使用连接池的UnpooledDataSource 当 的type属性为 ...

  6. 由 Vue 中三个常见问题引发的深度思考

    为什么 data 要写成函数,而不允许写成对象? Vue 中常说的数据劫持到底是什么? Vue 实例中数组改变 length 或下标直接赋值什么不能更新视图? http://www.sohu.com/ ...

  7. linux环境中关闭tomcat,通过shutdown.sh无法彻底关闭--线程池

    最近测试环境上测试的项目通过shutdown.sh始终无法彻底关闭. 之前临时解决方法两种: 第一:通过ps -ef|grep tomcat查看到tomcat的进程直接使用kill来杀死进程. 第二: ...

  8. Ubuntu 安装 JDK1.8

    以下是Ubuntu 14.04安装JDK1.8.0_25与配置环境变量过程笔记. 1.源码包准备: 首先到官网下载jdk,http://www.oracle.com/technetwork/java/ ...

  9. ci框架总结(一)

    在进行数据库操作前一定要先初始化数据库类:$this->load->database(); 在model类中: class Myiapp_model extends CI_Model{ p ...

  10. MySQL 如何使用show processlist进行过滤

    在使用show processlist的时候,直接使用会显示很多的内容,无法很快找到需要的信息. 如何过滤操作呢? 其实,show processlist展示的内容是从information_sche ...