字典树
用途:
1、给定字符串排序问题
2、字符串排序
结构体
struct Node {
int flag; //为1表示成词
struct Node *next[BASE];
} ;
Node *getNewNode() {
Node *p = (Node *)malloc(sizeof(Node));
p->flag = 0;
memset(p->next, 0, sizeof(p->next));
return p;
}
void insert(Node *root, const char *str) {
Node *p = root;
for (int i = 0; str[i]; i++) {
int ind = str[i] - 'a';
if (p->next[ind] == NULL) p->next[ind] = getNewNode();
p = p->next[ind];
}
p->flag++; //修改为子字符串的个数
return ;
}
int find(Node *root, const char *str) {
int res = 0;
Node *p = root;
for (int i = 0; str[i]; i++) {
int ind = str[i] - 'a';
p = p->next[ind];
if (p == NULL) break;
res += p->flag;
}
return res;
} //这个函数可用于 判断str的前缀有多少个