-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind2.c
118 lines (101 loc) · 3.07 KB
/
find2.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/***************************************************************************
* Find sub-words from dictionary *
***************************************************************************/
#include "d2.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define TRUE 1
#define FALSE 0
Dict dict;
/*-------------------------------------------------------------------------*
* types used *
*-------------------------------------------------------------------------*/
typedef struct {unsigned char count[26];} Pool;
#define Pool_contains(p,c) ((p).count[(c)-'A']>0)
#define Pool_add(p,c) ((p).count[(c)-'A']++)
#define Pool_remove(p,c) ((p).count[(c)-'A']--)
#define Pool_count(p,c) ((p).count[(c)-'A'])
#define Pool_clear(p) {int i; for (i=0; i<26; i++) (p).count[i]=0;}
/*-------------------------------------------------------------------------*
* Make a pool out of a word *
*-------------------------------------------------------------------------*/
int make_pool(const char *word, Pool *p)
{
int c, n=0;
Pool_clear(*p); /* clear the counts */
while ((c = *word++) != 0) {
if (isalpha(c)) {
Pool_add(*p, toupper(c));
n++;
}
}
return n;
}
/*-------------------------------------------------------------------------*
* Process all sub-words of the given length made from the pool *
*-------------------------------------------------------------------------*/
void find_subwords(Pool *wordpool, int sublen,
void process(const char *sw, Pool *rem))
{
if (sublen >= 2 && sublen <= MAXLEN) {
Dscan ds = Dscan_open(dict, sublen);
/* scan dictionary for each initial pair of letters */
const char *subword;
while ((subword = Dscan_read(ds)) != NULL) {
Pool remd = *wordpool;
int i, c, ok=TRUE;
for (i=0; i<sublen; i++) {
c = subword[i];
if (Pool_contains(remd, c)) {
Pool_remove(remd, c);
}else{
ok = FALSE;
Dscan_skip(ds, i);
break;
}
}
if (ok) {
process(subword, &remd);
}
}
Dscan_close(ds);
}
}
char word1[MAXLEN+1];
int len2;
void print_both(const char *word2, Pool *remd)
{
printf("%s %s\n", word1, word2);
}
void find_other_and_print(const char *word, Pool *remd)
{
strcpy(word1, word);
find_subwords(remd, len2, print_both);
}
int main(int argc, char **argv)
{
char word[100];
Pool wordpool;
int len, i;
dict = Dict_open(NULL);
if (argc > 1) {
len = make_pool(argv[1], &wordpool);
for (i=len; i>=(len+1)/2; i--) {
len2 = len-i;
find_subwords(&wordpool, i, find_other_and_print);
}
} else {
while (printf("word> "), fflush(stdout),
fgets(word, sizeof(word), stdin),
(len = make_pool(word, &wordpool)) != 0) {
for (i=len; i>=(len+1)/2; i--) {
len2 = len-i;
find_subwords(&wordpool, i, find_other_and_print);
}
}
}
Dict_close(dict);
return 0;
}