# 字典树

http://www.cnblogs.com/DiaoCow/archive/2010/04/19/1715337.html

#define  MAX    26 //字符集大小

typedef
struct TrieNode
{

int nCount; //记录该字符出现次数
struct TrieNode *next[MAX];
}TrieNode;

TrieNode Memory[
1000000];
int allocp = 0;

/*初始化*/
void InitTrieRoot(TrieNode **pRoot)
{

*pRoot = NULL;
}

/*创建新结点*/
TrieNode
*CreateTrieNode()
{

int i;
TrieNode
*p;

p
= &Memory[allocp++];
p
->nCount = 1;

for(i = 0 ; i < MAX ; i++)
{
p
->next[i] = NULL;
}

return p;
}

/*插入*/
void InsertTrie(TrieNode **pRoot , char *s)
{

int i , k;
TrieNode
*p;

if(!(p = *pRoot))
{
p
= *pRoot = CreateTrieNode();
}
i
= 0;

while(s[i])
{
k
= s[i++] - 'a'; //确定branch
if(p->next[k])
p
->next[k]->nCount++;

else
p
->next[k] = CreateTrieNode();
p
= p->next[k];
}
}

//查找
int SearchTrie(TrieNode **pRoot , char *s)
{
TrieNode
*p;

int i , k;

if(!(p = *pRoot))
{

return 0;
}
i
= 0;

while(s[i])
{
k
= s[i++] - 'a';

if(p->next[k] == NULL)    return 0;
p
= p->next[k];
}

return p->nCount;
}

1.统计难题(这里都用数组分配结点，用malloc分配太慢了）

#include <stdio.h>
#define  MAX    26

typedef
struct TrieNode
{

int nCount;

struct TrieNode *next[MAX];
}TrieNode;

TrieNode Memory[
1000000];
int allocp = 0;

void InitTrieRoot(TrieNode **pRoot)
{

*pRoot = NULL;
}

TrieNode
*CreateTrieNode()
{

int i;
TrieNode
*p;

p
= &Memory[allocp++];
p
->nCount = 1;

for(i = 0 ; i < MAX ; i++)
{
p
->next[i] = NULL;
}

return p;
}

void InsertTrie(TrieNode **pRoot , char *s)
{

int i , k;
TrieNode
*p;

if(!(p = *pRoot))
{
p
= *pRoot = CreateTrieNode();
}
i
= 0;

while(s[i])
{
k
= s[i++] - 'a'; //确定branch
if(p->next[k])
p
->next[k]->nCount++;

else
p
->next[k] = CreateTrieNode();
p
= p->next[k];
}
}

int SearchTrie(TrieNode **pRoot , char *s)
{
TrieNode
*p;

int i , k;

if(!(p = *pRoot))
{

return 0;
}
i
= 0;

while(s[i])
{
k
= s[i++] - 'a';

if(p->next[k] == NULL)    return 0;
p
= p->next[k];
}

return p->nCount;
}

int main(void)
{

char s[11];

TrieNode
*Root = NULL;
InitTrieRoot(
&Root);

while(gets(s) && s[0])
{
InsertTrie(
&Root , s);
}

while(gets(s))
{
printf(
"%d/n", SearchTrie(&Root , s));
}

2.Phone List

return    0;
}

#include <stdio.h>
#define  MAX    10

typedef
struct TrieNode
{

int nEndFlag; //标记该字符是否是某一字符串的结尾
struct TrieNode *next[MAX];
}TrieNode;

TrieNode Memory[
1000000];
int allocp = 0 , nFlag = 0;

void InitTrieRoot(TrieNode **pRoot)
{

*pRoot = NULL;
}

TrieNode
*CreateTrieNode()
{

int i;
TrieNode
*p;

p
= &Memory[allocp++];
p
->nEndFlag = 0;

for(i = 0 ; i < MAX ; i++)
{
p
->next[i] = NULL;
}

return p;
}

void InsertTrie(TrieNode **pRoot , char *s)
{

int i , k;
TrieNode
*p;

if(!(p = *pRoot))
{
p
= *pRoot = CreateTrieNode();
}
i
= 0;

while(s[i])
{
k
= s[i++] - '0';

if(p->next[k])
{

if(p->next[k]->nEndFlag == 1 || s[i] == '/0') //先短后长 || 先长后短
{
nFlag
= 1;

return;
}
}

else
{
p
->next[k] = CreateTrieNode();
}
p
= p->next[k];
}
p
->nEndFlag = 1//标记结尾
}

int main(void)
{

int  z , n , i;

char s[11];
TrieNode
*Root;

scanf(
"%d", &z);

while(z-- > 0
{
nFlag
= allocp = 0;
InitTrieRoot(
&Root);
scanf(
"%d" , &n);    getchar();

for(i = 0 ; i < n ; i++)
{
gets(s);

if(!nFlag)    InsertTrie(&Root , s);
}
nFlag
? printf("NO/n") : printf("YES/n");
}

return    0;
}

#### acm 算法 字典树模板

2018年05月29日 37KB 下载

#### 字典树的指针模板与数组模板

2015-03-06 18:12:59

#### Java实现字典树TrieTree

2015-08-19 13:04:45

#### 字典树详解----串查找、排序、公共前缀之杀手锏

2014-02-21 11:15:57

#### 字典树简介和简易应用

2013-03-31 16:15:27

#### Trie树（字典树）：应用于统计和排序

2017-04-10 15:44:59

#### 字典树数组实现

2014-08-06 21:04:05

#### 空间优化的字典树

2016-09-08 18:40:16

#### hdu1671-每个字典树都应该释放内存

2012-08-30 21:52:23

#### 01字典树专题

2016-05-23 19:26:51