字典.cpp

来自「字典树算法 算法描述为:由字母a~z所组成的字符串的一个集合中」· C++ 代码 · 共 55 行

CPP
55
字号
#include <stdio.h> 
#include <malloc.h> 

typedef struct tire 
{ 
struct tire *next[26]; 
char date; 
int cnt; 
}*_tire; 

void init_tire(_tire root, char *string) 
{ 
_tire s; 
s=root; 

while(*string!=’\0’) 
{ 
if(s->next[*string - ’a’]==NULL) 
{ 
s->next[*string - ’a’] = (_tire)malloc(sizeof(struct tire)); 
(s->next[*string - ’a’])->date = *string; 
s = s->next[*string - ’a’]; 
for(int i=0;i<26;i++) 
{ 
s->next[i] = NULL; 
} 
} 
else 
{ 
s = s->next[*string - ’a’]; 
} 
string++; 
} 
s->cnt=1; 
} 

void print(_tire root, char *s, int i) 
{ 
int j; 
s[i] = root->date; 

if(root->cnt==1) 
{ 
s[i+1] = 0; 
puts(s); 
} 

for(j=0;j<26;j++) 
{ 
if(root->next[j]!=NULL) 
{ 
print(root->next[j],s,i+1); 
} 
} 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?