-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.c
95 lines (82 loc) · 1.9 KB
/
test.c
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
86
87
88
89
90
91
92
93
94
95
#include <stddef.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "chtrie.h"
#define fatal(s) do { perror(s); exit(-1); } while (0)
#define N 65536
#define M 256
static char *dict1[] = { "", "the", "a", "an" };
static char *dict2[] = { "he", "she", "his", "hers" };
static char *dict3[] = { "this", "that" };
static char *stop[] = { "the", "an", "a" };
static chtrie *tr;
static int term[N]; /* is `i` a termination node */
static int nchild[N]; /* number of children of `i` */
static void add(char *s);
static void del(char *s);
static int query(char *s);
int main(void)
{
static char line[N];
int i, j;
if (!(tr = chtrie_alloc(N, M)))
fatal("chtrie_alloc");
for (i = 0; i < sizeof dict1 / sizeof dict1[0]; i++)
add(dict1[i]);
for (i = 0; i < sizeof dict2 / sizeof dict2[0]; i++)
add(dict2[i]);
for (i = 0; i < sizeof stop / sizeof stop[0]; i++)
del(stop[i]);
for (i = 0; i < sizeof dict3 / sizeof dict3[0]; i++)
add(dict3[i]);
while (fgets(line, sizeof line, stdin)) {
line[strcspn(line, "\n")] = '\0';
printf("%d\n", query(line) ? 1 : 0);
}
chtrie_free(tr);
return 0;
}
static void add(char *s)
{
int it;
it = 0;
while (*s) {
if (chtrie_walk(tr, it, (unsigned char)*s, 0) == -1)
nchild[it]++;
if ((it = chtrie_walk(tr, it, (unsigned char)*s, 1)) == -1)
fatal("chtrie_walk");
s++;
}
term[it] = 1;
}
static void del(char *s)
{
static int nodes[N], symbs[N], n; /* trace the path */
int it;
n = 0;
it = 0;
while (it >= 0 && *s) {
nodes[n] = it;
symbs[n] = *s;
n++;
it = chtrie_walk(tr, it, (unsigned char)*s++, 0);
}
if (it < 0 || !term[it])
return;
term[it] = 0;
while (it > 0 && !term[it] && nchild[it] == 0) {
n--;
chtrie_del(tr, nodes[n], symbs[n]);
it = nodes[n];
nchild[it]--;
}
}
static int query(char *s)
{
int it;
it = 0;
while (it >= 0 && *s)
it = chtrie_walk(tr, it, (unsigned char)*s++, 0);
return it >= 0 && term[it];
}