数据结构~trie树(字典树)
Posted lis-
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据结构~trie树(字典树)相关的知识,希望对你有一定的参考价值。
1、概述
Trie树,又称字典树,单词查找树或者前缀树,是一种用于快速检索的多叉树结构,如英文字母的字典树是一个26叉树,数字的字典树是一个10叉树。
我理解字典树是看了这位大佬博客。还不了解字典树的可以先进去学习一下
https://www.cnblogs.com/TheRoadToTheGold/p/6290732.html
还有这个讲了下为什么用字典树,和其他的相比优缺点在哪
https://www.cnblogs.com/Allen-rg/p/7128518.html
现在来个题来更进一步了解字典树吧 ,嘻嘻-_-
You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have a dictionary to help you understand them.
Input
Input consists of up to 100,000 dictionary entries, followed by a blank line, followed by a message of up to 100,000 words. Each dictionary entry is a line containing an English word, followed by a space and a foreign language word. No foreign word appears more than once in the dictionary. The message is a sequence of words in the foreign language, one word on each line. Each word in the input is a sequence of at most 10 lowercase letters.
Output
Output is the message translated to English, one word per line. Foreign words not in the dictionary should be translated as "eh".
Sample Input
dog ogday cat atcay pig igpay froot ootfray loops oopslay atcay ittenkay oopslay
Sample Output
cat eh loops
Hint
Huge input and output,scanf and printf are recommended.
题意:前面有个字典列表,后一个单词映射到前一个,后面有很多次查询,输出单词映射到的那个单词,如果没有输出eh
思路:因为这题数据比较弱,所以用map映射照样可以过,在这里我们当是字典树入门,之前我们用map可以算某个单词映射到哪个单词,这个字典树和map其实
相差不大,但是在求某个前缀的个数的时候,map就要使用多个映射,这个时候字典树的优势就来了
我们可以看下代码
#include<cstdio> #include<cstring> using namespace std; int top=0; int a[1000001][27]; int sum[1000001]; void insert(char str[]) { int root=0; for(int i=0;str[i]!=‘