小写字母的字典树
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85 |
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXN 1000 #define LEN 100 char
END[LEN] = "0" ; struct
node{ struct
node* next[26]; int
id; int
end; }; int
count; char
book[MAXN][LEN]; struct
node root; int
isin( struct
node *root, char
*tmp) { char
head; if (tmp[0] == ‘\0‘ ) { if (root->end == 1) { return
1; } else
{ return
0; } } head = *( char
*)tmp - ‘a‘ ; if (root->next[head] == NULL) { return
0; } return
isin(root->next[head], tmp + 1); } void
insert( struct
node *root, char
*tmp) { struct
node *t; if (tmp[0] == ‘\0‘ ) { root->end = 1; root->id = count; return ; } t = ( struct
node *) malloc ( sizeof ( struct
node)); t->id = -1; t->end = 0; memset (t->next, 0, sizeof (t->next)); root->next[*( char
*)tmp - ‘a‘ ] = t; insert(t, tmp + 1); return ; } int
main() { /* init */ char
tmp[LEN]; int
i; count = 0; memset (book, 0, sizeof (book)); root.id = -1; root.end = 0; memset (root.next, 0, sizeof (root.next)); while (1) { scanf ( "%s" , tmp); if ( strcmp (tmp, END) == 0) { break ; } if (isin(&root, tmp) == 1) { printf ( "already in\n" ); } else
{ strcpy (book[count], tmp); insert(&root, tmp); count ++; } } printf ( "now we have %d words:\n" , count); for (i = 0; i < count; i++) { printf ( "%s\n" , book[i]); } return
0; } |
原文:http://www.cnblogs.com/igloo1986/p/3521388.html