前面学了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. mysql恢复数据

    1.崩溃恢复: 突然断电.宕机,导致mysql无法正常启动: (1) 关闭数据库. (2) Vim /etc/my.cnf  添加:innodb_force_recovery=1   默认为0. 1( ...

  2. Django:调用css、image、js

    1.在项目的manage.py同级目录创建static.templates 2.编辑settings.py,在最后加入 STATIC_URL = '/static/' HERE = os.path.d ...

  3. Could not find conduit initiator for address:xxxxxxxxx and transport: http://schemas.xmlsoap.org/soap/http

    <properties> <cxf.version>3.1.12</cxf.version> </properties> <dependencie ...

  4. 大数据学习——yum安装tomcat

    https://www.cnblogs.com/jtlgb/p/5726161.html 安装tomcat6 yum install tomcat6 tomcat6-webapps tomcat6-a ...

  5. iOS第三方地图-百度地图常用功能使用(POI搜索,地理正反编码,定位,添加标注)

    百度地图官网: http://developer.baidu.com/map/index.php?title=iossdk 百度地图集成 1.引入相关包

  6. [CTSC2007]数据备份Backup 题解

    题意: 一维直线上有n个点,任取2k个互不相同的点组成k条链,求链的最小总长 思路: 1.最优时链不相交,相邻两两相减,将题目转化为:在n-1个数中取互不相邻的k个数使总和最小. 2.贪心取最小的“数 ...

  7. ASP.NET程序开发中经典常用的三十三种代码实例[确实有用]

    原文发布时间为:2008-11-10 -- 来源于本人的百度文章 [由搬家工具导入] ASP.NET程序开发中经典常用的三十三种代码实例:1. 打开新的窗口并传送参数: 传送参数:response.w ...

  8. eclispe使用

    eclipse 快捷键 ctrl+shif+o     :去除多余引用 ctrl+shift+x    :转大写 ctrl+shift+y    :转小写 ctrl+o :查找方法 Alt+ ← :回 ...

  9. python学习之-- Mysql 基础知识

    数据库介绍及MYSQL基础操作了解 关系型数据库(RDBMS)是按照数据结构来组织,存储和管理数据的仓库.特点:1:数据以表格的形式出现2:每行为各种记录名称3:每列为记录名称所对应的数据域4:许多的 ...

  10. HDU 6370 dfs+并查集

    Werewolf Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...