在计算机科学中,trie,又称前缀树或字典树,是一种有序树,用于保存关联数组,其中的键通常是字符串。与二叉查找树不同,键不是直接保存在节点中,而是由节点在树中的位置决定。一个节点的所有子孙都有相同的前缀,也就是这个节点对应的字符串,而根节点对应空字符串。一般情况下,不是所有的节点都有对应的值,只有叶子节点和部分内部节点所对应的键才有相关的值。
在hihocode中第二个题目就是trie树,题意如下:
描述
小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程的学习道路上一同前进。
这一天,他们遇到了一本词典,于是小Hi就向小Ho提出了那个经典的问题:“小Ho,你能不能对于每一个我给出的字符串,都在这个词典里面找到以这个字符串开头的所有单词呢?”
身经百战的小Ho答道:“怎么会不能呢!你每给我一个字符串,我就依次遍历词典里的所有单词,检查你给我的字符串是不是这个单词的前缀不就是了?”
小Hi笑道:“你啊,还是太年轻了!~假设这本词典里有10万个单词,我询问你一万次,你得要算到哪年哪月去?”
小Ho低头算了一算,看着那一堆堆的0,顿时感觉自己这辈子都要花在上面了...
小Hi看着小Ho的囧样,也是继续笑道:“让我来提高一下你的知识水平吧~你知道树这样一种数据结构么?”
小Ho想了想,说道:“知道~它是一种基础的数据结构,就像这里说的一样!”
小Hi满意的点了点头,说道:“那你知道我怎么样用一棵树来表示整个词典么?”
小Ho摇摇头表示自己不清楚。
“你看,我们现在得到了这样一棵树,那么你看,如果我给你一个字符串ap,你要怎么找到所有以ap开头的单词呢?”小Hi又开始考校小Ho。
“唔...一个个遍历所有的单词?”小Ho还是不忘自己最开始提出来的算法。
“笨!这棵树难道就白构建了!”小Hi教训完小Ho,继续道:“看好了!”
“那么现在!赶紧去用代码实现吧!”小Hi如是说道
输入
输入的第一行为一个正整数n,表示词典的大小,其后n行,每一行一个单词(不保证是英文单词,也有可能是火星文单词哦),单词由不超过10个的小写英文字母组成,可能存在相同的单词,此时应将其视作不同的单词。接下来的一行为一个正整数m,表示小Hi询问的次数,其后m行,每一行一个字符串,该字符串由不超过10个的小写英文字母组成,表示小Hi的一个询问。
在20%的数据中n, m<=10,词典的字母表大小<=2.
在60%的数据中n, m<=1000,词典的字母表大小<=5.
在100%的数据中n, m<=100000,词典的字母表大小<=26.
本题按通过的数据量排名哦~
输出
对于小Hi的每一个询问,输出一个整数Ans,表示词典中以小Hi给出的字符串为前缀的单词的个数。
5 babaab babbbaaaa abba aaaaabaa babaababb 5 babb baabaaa bab bb bbabbaab
1 0 3 0 0结题思想:
输入时 就是把树存起来,然后每增加一个单词,对应数的节点的degree+1,。
查找时,如果找到单词最后一个字母还在树中,输出单词对应树节点的degree,否则不在树中,输出0.
这里有两种存储方法 : 方法1 每个节点的孩子个数不会超过26个,因为只有26个英文字母,试了这种方法,占用的内存相对较多
方法2 每个节点的第一个孩子记录着所有的兄弟方法,这样如果某个节点只有两个孩子,只需要两个节点保存,而不是26个。
最后运行发现同样的测试用例 方法1 用了71M内存,方法2只用了20M内存。
方法2 代码如下(水平有限,欢迎指正):
#include<iostream>
#include<string.h>
#include<deque>
using namespace std;
typedef struct node {
char data;
struct node * child;
struct node * brother;
int degree;
} NODE;
void insert(NODE & root, char s[])
{
// cout<<s<<endl;
int len = strlen(s);
if(len == 0){
return;
}
NODE * point = &root;
int i;
for(i = 0; i < len ;i++){
//if this node not has child , new a new child.
if(point->child == NULL){
NODE * n1 = new NODE();
n1->child = NULL;
n1->brother = NULL;
n1->degree = 1;
n1->data = s[i];
point->child = n1;
point = n1;
}
//
else{
NODE * child = point->child;
while(child != NULL) {
if(child->data == s[i]){
child->degree ++;
point = child;
break;
}
else{
point = child;
child = child->brother;
}
}
if(child == NULL) {
NODE * n1 = new NODE();
n1->child = NULL;
n1->brother = NULL;
n1->degree = 1;
n1->data = s[i];
point->brother = n1;
point = n1;
}
}
}
}
int read(NODE & root ,char s[]){
NODE * pos = &root;
int len = strlen(s) ;
if(pos->child == NULL)
return 0;
if(len <= 0)
return 0;
int i;
for(i = 0; i< len; i++) {
NODE * child = pos->child;
while(child != NULL){
if(child->data == s[i]){
pos = child;
if(i >= len -1){
return child->degree;
}
break;
}
child = child->brother;
}
if(child == NULL){
return 0;
}
}
return 0;
}
int main(void) {
NODE head;
head.degree = 0;
head.child = NULL;
head.brother = NULL;
int n,m;
char word[11];
cin>>n;
while(n--){
cin>>word;
insert( head,word);
}
cin>>m;
while(m--)
{
cin>>word;
int num = read(head,word);
cout<<num<<endl;
}
return 0;
}
方法1 (网上直接粘贴的,操作相对简单)
#include <iostream>
#include <cstring>
using namespace std;
typedef struct node_
{
void * next[26];
bool isNull;
int num;
}node;
node* mallocNode()
{
int i;
node * tempNode = new(node);
//node * tempNode = (node *)malloc(sizeof(node));
if(tempNode == NULL)
{
cout<<"malloc failed!"<<endl;
return NULL;
}
tempNode->isNull = true;
tempNode->num = 0;
for(i = 0; i < 26; i++)
{
tempNode->next[i] = NULL;
}
return tempNode;
}
int main()
{
int wordNum,askNum;
int i,j,k,wordSize;
int value;
node * root = mallocNode();
root->isNull = false;
node * curPos = NULL;
char word[16];
cin >> wordNum;
i = wordNum;
while(i--)
{
cin >> word;
wordSize = strlen(word);
curPos = root;
for(j = 0; j < wordSize; j++)
{
value = word[j] - 'a';
//第一次出现此字母
if(curPos->next[value] == NULL)
{
curPos->next[value] = mallocNode();
curPos = (node *)curPos->next[value];
curPos->num = 1;
}
else
{
curPos = (node*)curPos->next[value];
curPos->num++;
}
}
}
cin>> askNum;
i = askNum;
while(i--)
{
cin >> word;
wordSize = strlen(word);
curPos = root;
for(j = 0; j < wordSize; j++)
{
value = word[j] - 'a';
if(curPos->next[value] == NULL)
{
cout<<0<<endl;
break;
}
else
{
curPos = (node *)curPos->next[value];
if(j >= wordSize - 1)
{
cout<<curPos->num<<endl;
}
}
}
}
}