Trie树

  1. Trie,又称单词查找树或键树,是一种树形结构,是一种哈希树的变种。利用字符串的公共前缀来降低查询时间的开销以达到提高效率的目的。可以统计词频或是判断是否是前缀。
  2. 1000万的广告库关键词,来一个查询词,找出是查询词子集的关键词集合。
 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <stdlib.h>
 4 
 5 #define MAX 256
 6 #define MAXLEN 256
 7 
 8 typedef struct _TrieNode
 9 {
10     int count;
11     struct _TrieNode *next[MAX];
12 }TrieNode;
13 
14 void Insert(char* word, TrieNode* root)
15 {
16     if (word[0] == '\0') return;
17     TrieNode* cur = root;
18     for (int i = 0; word[i] != '\0'; ++i)
19     {
20         if (cur->next[word[i]] == NULL)
21         {
22             TrieNode* newNode = (TrieNode*)malloc(sizeof(TrieNode));
23             memset(newNode, 0, sizeof(TrieNode));
24             cur->next[word[i]] = newNode;
25         }
26         cur = cur->next[word[i]];
27     }
28     cur->count++;
29 }
30 
31 void Construct(TrieNode* &root)
32 {
33     char inStr[MAXLEN];
34     root = (TrieNode*)malloc(sizeof(TrieNode));
35     memset(root, 0, sizeof(TrieNode));
36     while (true)
37     {
38         scanf("%s", inStr);
39         if (strcmp(inStr, "*") == 0)
40             break;
41         Insert(inStr, root);
42     }
43 }
44 
45 int Find(TrieNode* root, char* word)
46 {
47     TrieNode* cur = root;
48     for (int i = 0; word[i] != '\0'; ++i)
49     {
50         if (cur->next[word[i]] == NULL)
51             return 0;
52         cur = cur->next[word[i]];
53     }
54     return cur->count;
55 }
56 
57 int main()
58 {
59     TrieNode* root;
60     char str[MAXLEN];
61     Construct(root);
62     printf("\n");
63     while (true)
64     {
65         scanf("%s", str);
66         if (strcmp(str, "*") == 0)
67             break;
68         printf("%s:%d\n", str, Find(root, str));
69     }
70     return 0;
71 }

 

posted on 2013-06-20 10:50  月moon鸟  阅读(229)  评论(0编辑  收藏  举报

导航