Timus 1545. Hieroglyphs Trie的即学即用 实现字典提示功能
前面学了Trie,那么就即学即用。运用Trie数据结构来解决这道题目。
本题目比較简单,当然能够不使用Trie。只是多用高级数据结构还是非常有优点的。
题目:
Latin letters. He wants to type hieroglyphs from his keyboard. His team-mate Sergey, in order to help Vova, created an applet that makes it possible to write hieroglyphs by means of typing Latin letters on the keyboard. Each hieroglyph is represented by a
sequence of two Latin letters. This correspondence is given in a special reference book compiled by Sergey. When the applet realizes that a sequence of Latin letters corresponding to a hieroglyph has been typed, it replaces the sequence with this hieroglyph.
automatically supply a prompt helping to continue this letter to a sequence representing a hieroglyph.
Input
corresponding to a hieroglyph. The next line contains a lowercase Latin letter entered by Vova.
Output
Sample
input | output |
---|---|
6 |
ka |
本题就是实现一个简单的字典提示功能,能够使用hash表的方法来做。实现起来也非常easy。
这里我做了个Trie Class来实现:
#include <iostream>
using namespace std; #define ALPHA_SIZE 26
#define CHAR_TO_INDEX(c) (c - 'a') struct HieroglyphsTrie_Node
{
int val;
HieroglyphsTrie_Node *children[ALPHA_SIZE];
}; struct HieroglyphsTrie
{
HieroglyphsTrie_Node *root;
int size;
}; class HieroglyphsTrieClass
{
HieroglyphsTrie *pTrie;
public:
HieroglyphsTrieClass()
{
pTrie = (HieroglyphsTrie *) malloc (sizeof(HieroglyphsTrie));
init();
} HieroglyphsTrie_Node *getNode()
{
HieroglyphsTrie_Node *pNode = nullptr;
pNode = (HieroglyphsTrie_Node *)malloc(sizeof(HieroglyphsTrie_Node));
if (pNode)
{
pNode->val = 0;
for (int i = 0; i < ALPHA_SIZE; i++)
{
pNode->children[i] = nullptr;
}
}
return pNode;
} void init()
{
pTrie->root = getNode();
pTrie->size = 0;
} void insert(const char key[])
{
int len = strlen(key);
int id = 0;
HieroglyphsTrie_Node *pCrawl = pTrie->root;
pTrie->size++;
for (int lv = 0; lv < len; lv++)
{
id = CHAR_TO_INDEX(key[lv]);
if (!pCrawl->children[id])
{
pCrawl->children[id] = getNode();
}
pCrawl = pCrawl->children[id];
}
pCrawl->val = pTrie->size;
} int search(char key[])
{
int len = strlen(key);
int id = 0;
HieroglyphsTrie_Node *pCrawl = pTrie->root;
for (int lv = 0; lv < len; lv++)
{
id = CHAR_TO_INDEX(key[lv]);
if (!pCrawl->children[id]) return 0;
pCrawl = pCrawl->children[id];
}
return (int)(0 != pCrawl->val);
} void HieroglyphsRun()
{
int n = 0;
cin>>n;
char ch[3];//不能是ch[2],由于后面还要多一个'\0'终止符
while (n--)
{
cin>>ch;
insert(ch);
}
char k;
cin>>k; HieroglyphsTrie_Node *pCrawl = nullptr;
pCrawl = pTrie->root->children[CHAR_TO_INDEX(k)];
if (pCrawl)
{
for (int i = 0; i < ALPHA_SIZE; i++)
{
if (pCrawl->children[i])
{
cout<<k<<char('a'+i)<<endl;
}
}
}
}
}; int main()
{
HieroglyphsTrieClass hie;
hie.HieroglyphsRun();
return 0;
}
本类临时还是不是完好的,慢慢完好吧,只是能够非常好完毕本题了。
Timus 1545. Hieroglyphs Trie的即学即用 实现字典提示功能的更多相关文章
- python利用Trie(前缀树)实现搜索引擎中关键字输入提示(学习Hash Trie和Double-array Trie)
python利用Trie(前缀树)实现搜索引擎中关键字输入提示(学习Hash Trie和Double-array Trie) 主要包括两部分内容:(1)利用python中的dict实现Trie:(2) ...
- 用trie树实现输入提示功能,输入php函数名,提示php函数
参照刘汝佳的trie树 结构体 #include "stdio.h" #include "stdlib.h" #include "string.h&q ...
- Trie|如何用字典树实现搜索引擎的关键词提示功能
Trie字典树 Trie字典树又称前缀树,顾名思义,是查询前缀匹配的一种树形数据结构 可以分为插入(创建) 和 查询两部分.参考地址极客时间 下图为插入字符串的过程: 创建完成后,每个字符串最后一个字 ...
- 老猿学5G随笔:5G网元功能体NF以及NF之间的两种接口--服务化接口和参考点
一.5G功能体之间的接口类型 5G不同功能体之间提供了两种接口: 服务化接口:Service-basedinterface,这个是类似微服务化架构的服务注册和服务发现来实现的功能体对外暴露的接口,这种 ...
- 【学习笔记】--- 老男孩学Python,day6 字典
详细方法:http://www.runoob.com/python/python-dictionary.html 1. dict 用大括号{} 括起来. 内部使用key:value的形式来保存数据 { ...
- idou老师教你学Istio 19 : Istio 流量治理功能原理与实战
一.负载均衡算法原理与实战 负载均衡算法(load balancing algorithm),定义了几种基本的流量分发方式,在Istio中一共有4种标准负载均衡算法. •Round_Robin: 轮询 ...
- idou老师带教你学Istio 03: istio故障注入功能的介绍和使用
故障注入测试 故障注入测试顾名思义就是当被测试应用部分组件或功能出现潜在故障时其本身的容错机制是否正常工作,以达到规避故障保证正常组件或功能的使用.Istio提供了HTTP故障注入功能,在http请求 ...
- 【转】B树、B-树、B+树、B*树、红黑树、 二叉排序树、trie树Double Array 字典查找树简介
B 树 即二叉搜索树: 1.所有非叶子结点至多拥有两个儿子(Left和Right): 2.所有结点存储一个关键字: 3.非叶子结点的左指针指向小于其关键字的子树,右指针指向大于其关键字的子树: 如: ...
- 数据结构 | 30行代码,手把手带你实现Trie树
本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是算法和数据结构专题的第28篇文章,我们一起来聊聊一个经典的字符串处理数据结构--Trie. 在之前的4篇文章当中我们介绍了关于博弈论的 ...
随机推荐
- vue 项目部署
vue项目部署到PHP项目 入口目录 vue项目打包后, 是一个单文件html 我们只需要把打包后的文件夹放在php项目的public下面 访问 xxx.com/h5/index.html 就可以访问 ...
- Java 关于循环的练习--和为n的正数序列
要求:输入一个正数n,输出所有和为n的连续正数序列. 分析可以从1开始连续加,若到i的和等于n则输出1到i之间的连续正数,若到i的和大于n,则改为从2开始连续加,再判断到i的和是否等于n,等于则输出2 ...
- Leetcode 260.只出现一次的数字III
只出现一次的数字III 给定一个整数数组 nums,其中恰好有两个元素只出现一次,其余所有元素均出现两次. 找出只出现一次的那两个元素. 示例 : 输入: [1,2,1,3,2,5] 输出: [3,5 ...
- zoj 2812
Quicksum Time Limit: 2 Seconds Memory Limit: 65536 KB A checksum is an algorithm that scans a p ...
- 潘多拉的盒子(bzoj 1194)
Description Input 第一行是一个正整数S,表示宝盒上咒语机的个数,(1≤S≤50).文件以下分为S块,每一块描述一个咒语机,按照咒语机0,咒语机1„„咒语机S-1的顺序描述.每一块的格 ...
- [USACO08OPEN]牛的车Cow Cars
题目描述 N (1 <= N <= 50,000) cows conveniently numbered 1..N are driving in separate cars along a ...
- 16.1117 NOIP 模拟赛
水灾(sliker.cpp/c/pas) 1000MS 64MB 大雨应经下了几天雨,却还是没有停的样子.土豪CCY刚从外地赚完1e元回来,知道不久除了自己别墅,其他的地方都将会被洪水淹没. CCY ...
- DNS Prefetch 【DNS 预解析技术】
DNS 实现域名到IP的映射.通过域名访问站点,每次请求都要做DNS解析.目前每次DNS解析,通常在200ms以下.针对DNS解析耗时问题,一些浏览器通过DNS Prefetch 来提高访问的流畅性. ...
- python学习之-- redis模块操作 LIST
redis 模块操作之 List List 操作,在内存中按照一个name对应一个List来存储. lpush(name,values):在name对应的list中添加元素,每个新的元素都添加到列表的 ...
- Spring MVC中 log4j日志文件配置相对路径
log4j和web.xml配置webAppRootKey 的问题 1 在web.xml配置 <context-param> <param-name>webAppRootKey ...