Timus 1545. Hieroglyphs Trie的即学即用 实现字典提示功能

前面学了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;
}

本类临时还是不是完好的,慢慢完好吧,只是能够非常好完毕本题了。

时间: 2024-08-04 18:49:48

Timus 1545. Hieroglyphs Trie的即学即用 实现字典提示功能的相关文章

URAL 1545. Hieroglyphs

1545. Hieroglyphs Time limit: 1.0 second Memory limit: 64 MB 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 ha

python第二天学了列表,字典和嵌套

1 #!/user/bin/env python 2 # -*- coding:utf-8 -*- 3 __author__ = 'Howie' 4 names = ['ZhangSan', 'LiSi', 'WangWu',['lili','dddd'], 'ZhaoLiu','DAda'] 5 #A = names[-2:] #如果想取倒数几个数需要吧最后一个省略 6 A = names[:3] #如果想取第一个开始到第几个数可以把索引值省略 7 print(A) 8 names.appen

结合目前所学的知识做一个用户注册功能,数据入到数据库

在html中 第一步:建立表格 第二步:建立表单 注意: 1.( method="POST"是把表单数据传送给PHP文件): 2.在input中,name是PHP中的表名,value是传送的值: 在PHP中 第一步:连接数据库: 第二步:用数据库: 第三步:设置编码: 第四步:在cmd中创建表:. 注意: 在创建表之前: 1. 登录MySQL服务器:mysql -hlocalhost -P3306 -uroot -p 2.创建数据库:create database php2016; 3

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

故障注入测试 故障注入测试顾名思义就是当被测试应用部分组件或功能出现潜在故障时其本身的容错机制是否正常工作,以达到规避故障保证正常组件或功能的使用.Istio提供了HTTP故障注入功能,在http请求转发的过程中,用户可以设定一个或多个故障.故障注入的修改作用于Virtual Service,共有两种不同的故障模式abort和delay. 类型 所属 描述 abort HTTPFaultInjection.Abort 中断Http请求并且返回既定的错误状态码给请求方 delay HTTPFaul

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

一.负载均衡算法原理与实战 负载均衡算法(load balancing algorithm),定义了几种基本的流量分发方式,在Istio中一共有4种标准负载均衡算法. ?Round_Robin: 轮询算法,顾名思义请求将会依次发给每一个实例,来共同分担所有的请求. ?Random: 随机算法,将所有的请求随机分发给健康的实例 ?Least_Conn: 最小连接数,在所有健康的实例中任选两个,将请求发给连接数较小的那一个实例. 接下来,我们将根据以上几个算法结合APM(应用性能管理)的监控拓扑图来

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

B  树 即二叉搜索树: 1.所有非叶子结点至多拥有两个儿子(Left和Right): 2.所有结点存储一个关键字: 3.非叶子结点的左指针指向小于其关键字的子树,右指针指向大于其关键字的子树: 如: B树的搜索,从根结点开始,如果查询的关键字与结点的关键字相等,那么就命中:否则,如果查询关键字比结点关键字小,就进入左儿子:如果比结点关键字大,就进入右儿子:如果左儿子或右儿子的指针为空,则报告找不到相应的关键字: 如果B树的所有非叶子结点的左右子树的结点数目均保持差不多(平衡),那么B树的搜索性

猫猫学iOS 之微博项目实战(2)微博主框架-自己定义导航控制器NavigationController

猫猫分享,必须精品 原创文章,欢迎转载.转载请注明:翟乃玉的博客 地址:http://blog.csdn.net/u013357243?viewmode=contents 一:加入导航控制器 上一篇博客完毕了对底部的TabBar的设置,这一章我们完毕自己定义导航控制器(NYNavigationController). 为啥要做自己定义呢.由于为了更好地封装代码,而且系统的UINavigationController不能满足我们的需求了,所以得自己定义. 首先,我们在NYTabBarViewCon

从零开始学android&lt;AutoCompleteTextView随笔提示文本框.十九.&gt;

随笔提示功能可以很好的帮助用户进行方便的信息输入,而在Android之中也提供了与之类似的功能,而这个功能的实现就需要依靠android.widget.AutoCompleteTextView类完成,此类的继承结构如下: java.lang.Object ? android.view.View ? android.widget.TextView ? android.widget.EditText ? android.widget.AutoCompleteTextView No. 方法 类型 描述

leetcode Implement Trie (Prefix Tree)

题目连接 https://leetcode.com/problems/implement-trie-prefix-tree/ Implement Trie (Prefix Tree) Description Implement a trie with insert, search, and startsWith methods. 字典树.. class TrieNode { public: // Initialize your data structure here. bool vis; Tri