前面学了Trie,那么就即学即用。运用Trie数据结构来解决这道题目。

本题目比較简单,当然能够不使用Trie。只是多用高级数据结构还是非常有优点的。

题目:

Vova is fond of anime. He is so enthusiastic about this art that he learned to communicate with his Japanese friends using their native language. However, for writing email messages Vova has to use
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.
When Vova started using Sergey's program, he quickly became bored of looking into the reference book so often. Help Sergey to upgrade the applet in such a way that for each typed Latin letter it would
automatically supply a prompt helping to continue this letter to a sequence representing a hieroglyph.

Input

The first line contains the number of hieroglyphs in Sergey's reference book N (1 ≤ N ≤ 1000). Each of the next N lines contains a sequence of two lowercase Latin letters
corresponding to a hieroglyph. The next line contains a lowercase Latin letter entered by Vova.

Output

Output sequences from the reference book that start with the given letter, one sequence per line, in an arbitrary order. If there are no such sequences, then output nothing.

Sample

input output
6
na
no
ni
ki
ka
ku
k
ka
ki
ku

本题就是实现一个简单的字典提示功能,能够使用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的即学即用 实现字典提示功能的更多相关文章

  1. python利用Trie(前缀树)实现搜索引擎中关键字输入提示(学习Hash Trie和Double-array Trie)

    python利用Trie(前缀树)实现搜索引擎中关键字输入提示(学习Hash Trie和Double-array Trie) 主要包括两部分内容:(1)利用python中的dict实现Trie:(2) ...

  2. 用trie树实现输入提示功能,输入php函数名,提示php函数

    参照刘汝佳的trie树 结构体 #include "stdio.h" #include "stdlib.h" #include "string.h&q ...

  3. Trie|如何用字典树实现搜索引擎的关键词提示功能

    Trie字典树 Trie字典树又称前缀树,顾名思义,是查询前缀匹配的一种树形数据结构 可以分为插入(创建) 和 查询两部分.参考地址极客时间 下图为插入字符串的过程: 创建完成后,每个字符串最后一个字 ...

  4. 老猿学5G随笔:5G网元功能体NF以及NF之间的两种接口--服务化接口和参考点

    一.5G功能体之间的接口类型 5G不同功能体之间提供了两种接口: 服务化接口:Service-basedinterface,这个是类似微服务化架构的服务注册和服务发现来实现的功能体对外暴露的接口,这种 ...

  5. 【学习笔记】--- 老男孩学Python,day6 字典

    详细方法:http://www.runoob.com/python/python-dictionary.html 1. dict 用大括号{} 括起来. 内部使用key:value的形式来保存数据 { ...

  6. idou老师教你学Istio 19 : Istio 流量治理功能原理与实战

    一.负载均衡算法原理与实战 负载均衡算法(load balancing algorithm),定义了几种基本的流量分发方式,在Istio中一共有4种标准负载均衡算法. •Round_Robin: 轮询 ...

  7. idou老师带教你学Istio 03: istio故障注入功能的介绍和使用

    故障注入测试 故障注入测试顾名思义就是当被测试应用部分组件或功能出现潜在故障时其本身的容错机制是否正常工作,以达到规避故障保证正常组件或功能的使用.Istio提供了HTTP故障注入功能,在http请求 ...

  8. 【转】B树、B-树、B+树、B*树、红黑树、 二叉排序树、trie树Double Array 字典查找树简介

    B  树 即二叉搜索树: 1.所有非叶子结点至多拥有两个儿子(Left和Right): 2.所有结点存储一个关键字: 3.非叶子结点的左指针指向小于其关键字的子树,右指针指向大于其关键字的子树: 如: ...

  9. 数据结构 | 30行代码,手把手带你实现Trie树

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是算法和数据结构专题的第28篇文章,我们一起来聊聊一个经典的字符串处理数据结构--Trie. 在之前的4篇文章当中我们介绍了关于博弈论的 ...

随机推荐

  1. JavaScript基础对象---Number

    一.创建Number实例对象 /** * new Number(value); * value 被创建对象的数字值 * * Number 对象主要用于: 如果参数无法被转换为数字,则返回 NaN. 在 ...

  2. Nginx配置ThinkPHP和Laravel虚拟主机

    ThinkPHP server { listen 443 ssl; server_name abc.com; root /var/www/abc; ssl on; ssl_certificate /e ...

  3. 嵩天老师的零基础Python笔记:https://www.bilibili.com/video/av13570243/?from=search&seid=15873837810484552531 中的1-14讲

    #coding=gbk#嵩天老师的零基础Python笔记:https://www.bilibili.com/video/av13570243/?from=search&seid=1587383 ...

  4. Android-Intent and Intent Filters

    1.intent(意图)可以用来创建启动3种类型的基本情况:①To start an activity:启动一个活动②To start an service③To start an broadcast ...

  5. Flask 架构 --xunfeng实例研究

    文件结构 │ Config.py # 配置文件 │ README.md # 说明文档 │ Run.bat # Windows启动服务 │ Run.py # webserver │ Run.sh # L ...

  6. 动手实操(一):如何用七牛云 API 实现相片地图?

    实操玩家: 在苹果手机上,我们只要打开定位服务,拍照后便能在相簿中找到地图,地图上显示着在各地拍摄的相片.网站上这种显示方式也并不少见,例如 Flickr.即将关闭的 Panoramio 等. 作为地 ...

  7. [HNOI2009]梦幻布丁(链表+启发式合并)

    洛谷传送门 开始一个O(n^2)思路,每次每句要改变颜色的点,改变完颜色后重新计算颜色的段数,显然拉闸. 然后呢..然后就不会了. 看了别人博客,才知道有个叫做启发式合并的东西,就是把小的合并到大的上 ...

  8. 约分差束 例题 ZOJ 2770 火烧连营

    题目来源:ZOJ Monthly, October 2006, ZOJ2770题目描述:大家都知道,三国时期,蜀国刘备被吴国大都督陆逊打败了.刘备失败的原因是刘备的错误决策.他把军队分成几十个大营,每 ...

  9. OC-runtime 的温习

    -.runtime简介 runtime简称运行时,OC就是运行时机制,也就是运行时的一些机制,其中最主要的是消息机制: 对于C语言,函数的调用在编辑的时候,会决定调用哪个函数: 对于OC的函数,属于动 ...

  10. Topcoder 658 650 point

    Topcoder 658 div2 500 加强版 不过给了<=20,暴力肯定不行. 然后想DP方程,先二分可能需要的最大次数mid; 然后根据 mid 构造 DP方程. 假设x[i]需要 x个 ...