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. learning gcc __BEGIN_DECLS and __END_DECLS

    __BEGIN_DECLS and  __END_DECLS  be use for mix C and C++

  2. 路由器配置——OSPF协议(1)

    一.实验目的:用OSPF协议使全网互通 二.拓扑图 三.具体步骤配置 (1)R1路由器配置 Router>enableRouter#configure terminalEnter configu ...

  3. c str to float

    #include <wchar.h> int main () { wchar_t szOrbits[] = L"365.24 29.53"; wchar_t * pEn ...

  4. centernet 相关

    1.下代码 git clone https://github.com/Duankaiwen/CenterNet.git 2.

  5. Netfilter 之 钩子函数调用

    本篇主要从三层协议栈调用函数NF_HOOK说起,不断深入,分析某个钩子点中所有钩子函数的调用流程,但是本文不包含规则介绍和核心的规则匹配流程,后续文章将继续分析: NF_HOOK函数先调用了nf_ho ...

  6. 线上应用接入sentinel的第一个流控规则

    sentinel接入第1个应用A以及控制台,已经上线一段时间了,本周接入了第2个应用B: 因为测试同学只有几个,没有压测团队.测试平台.. 各接口能承载的最大qps不确定 ,接入的应用暂时都没有配置规 ...

  7. LC 973. K Closest Points to Origin

    We have a list of points on the plane.  Find the K closest points to the origin (0, 0). (Here, the d ...

  8. 发布机制-灰度发布-例子:Windows

    ylbtech-发布机制-灰度发布-例子:Windows 在传统软件产品发布过程中(例如微软的Windows 7的发布过程中),一般都会经历Pre-Alpha.Alpha.Beta.Release c ...

  9. AnimationUtil

    import android.view.View; import android.view.animation.AlphaAnimation; public class AnimationUtil { ...

  10. selector状态选择器

    Selector selector就是状态选择器(StateList),它分为两种,一种Color-Selector 和Drawable-Selector. Color-Selector color- ...