字典树

字典树,又称为单词查找树,Trie树, 是一种树形结构,是一种哈希树的变种。典型应用是用于统计,排序和保存大量的字符串(但是不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计。它的优点是:利用字符串的公共前缀来节约存储空间,最大限度的减少无畏的字符串比较,查询效率比哈希表高。

对于树形结构的理解我认为,最重要的是知道他的模型;例如字典树的模型如下:

在字典树中最重要的操作就是建立字典树,这也是有关字典树问题的基础,只有会建树了以后,才可以根据题意来决定如何去查找。

建树的操作如下(针对只考虑小写字母):

void insert(char str[])
{
	int len, cur;
	node *head = a;
	len = strlen(str);
	for(int i = 0; i < len; i++)
	{
		cur = (int)(str[i] - 'a');
		if(head->tire[cur] == NULL)
		{
			head->tire[cur] = new node;
			head = head->tire[cur];
			head->count = 1;
			for(int j = 0; j < 26; j++)
				head->tire[j] = NULL;
		}
		else
		{
			head = head->tire[cur];
			head->count++;
		}
	}
}

利用字典树处理的典型题:

hdu1251, poj2001;

对于hdu1251,它的主要操作就是:插入, 删除

1:在插入时, 如果遇到字母,没出现过,初始化为1,出现过的话count++;通过head->count来记录从根节点到head结点的前缀出现的次数。

2:   返回单词结尾字母的count就是前缀出现的次数。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;


struct Dictree
{
	int count;
	Dictree *tire[26];
}*a;

void init()
{
	a = new Dictree;
	for(int i = 0; i < 26; i++)
		a->tire[i] = NULL;
}

void insert(char str[])
{
	int len, cur;
	Dictree *head = a;
	len = strlen(str);
	for(int i = 0; i < len; i++)
	{
		cur = (int)(str[i] - 'a');
		if(head->tire[cur] == NULL)
		{
			head->tire[cur] = new Dictree;
			head = head->tire[cur];
			head->count = 1;
			for(int j = 0; j < 26; j++)
				head->tire[j] = NULL;
		}
		else
		{
			head = head->tire[cur];
			head->count++;
		}
	}
}

int search(char str[])
{
	int len, cur;
	Dictree *head = a;
	len = strlen(str);
	for(int i = 0; i < len; i++)
	{
		cur = (int)(str[i] - 97);
		if(head -> tire[cur] ==NULL)
			return 0;
		else
			head = head->tire[cur];
	}
	return head->count;
}
int main()
{
	char str[11], que[11];
	int temp, query, count;
	init(); 
	while(gets(str) && strcmp(str, "") != 0)
		insert(str);
	while(scanf("%s", que) != EOF)
	{
		count = search(que);
		printf("%d\n",count);
	}
	return 0;
}

对于poj2001来说,他就是在上面的查询中修改就可以;由于要求缩写, 但是该缩写是独一无二的,也就是说他的出现的次数count是1次,所以对于每个单词来说,就是从前往后查询,当出现当前字母的count为1时输出,并且break就好了。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 1000+10;
char list[maxn][25];
struct node
{
	int count;
	node *tire[26];
}*a;

void init()
{
	a = new node;
	for(int i = 0; i < 26; i++)
		a->tire[i] = NULL;
}

void insert(char str[])
{
	int len, cur;
	node *head = a;
	len = strlen(str);
	for(int i = 0; i < len; i++)
	{
		cur = (int)(str[i] - 'a');
		if(head->tire[cur] == NULL)
		{
			head->tire[cur] = new node;
			head = head->tire[cur];
			head->count = 1;
			for(int j = 0; j < 26; j++)
				head->tire[j] = NULL;
		}
		else
		{
			head = head->tire[cur];
			head->count++;
		}
	}
}

int search(char str[])
{
	int len, m;
	node *head = a;
	len = strlen(str);
	char ans[25];
	for(int i = 0; i < len; i++)
	{
		m = str[i] - 'a';
		head = head->tire[m];
		ans[i] = str[i];
		ans[i+1] = '\0';
		if(head->count == 1)
		{
			printf("%s %s\n",str, ans);
			return 0;
		}
	}
	printf("%s %s\n",str, ans);
}
int main()
{
	int temp, query, count, t=0;
	init(); 
	while(scanf("%s",list[t]) != EOF)
	{
		insert(list[t]);
		t++;
	}
	for(int i = 0; i < t; i++)
	{
		search(list[i]);
	}
	return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值