-
Notifications
You must be signed in to change notification settings - Fork 0
/
find1.c
126 lines (110 loc) · 3.19 KB
/
find1.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
119
120
121
122
123
124
125
126
/***************************************************************************
* 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;}
int Pool_size(Pool *p)
{
int num=0, i;
for (i=0; i<26; i++) {
num += p->count[i];
}
return num;
}
/*-------------------------------------------------------------------------*
* Make a pool out of a word *
*-------------------------------------------------------------------------*/
void Pool_addword(Pool *p, const char *word)
{
int c;
while ((c = *word++) != 0) {
if (isalpha(c)) {
Pool_add(*p, toupper(c));
}
}
}
/*-------------------------------------------------------------------------*
* 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);
}
}
void print_word_and_remainder(const char *word, Pool *remd)
{
int c, n;
printf("%s\t", word);
for (c='A'; c<='Z'; c++) {
for (n=Pool_count(*remd,c); n>0; n--) {
putchar(c);
}
}
putchar('\n');
}
int main(int argc, char **argv)
{
char word[100];
Pool wordpool;
int len, i;
dict = Dict_open(NULL);
Pool_clear(wordpool);
if (argc == 1) {
while (printf("word> "), fflush(stdout),
fgets(word, sizeof(word), stdin)) {
Pool_addword(&wordpool, word);
len = Pool_size(&wordpool);
if (len == 0) break;
for (i=len; i>=0; i--) {
find_subwords(&wordpool, i, print_word_and_remainder);
}
}
} else {
for (i=1; i<argc; i++) {
Pool_addword(&wordpool, argv[i]);
}
len = Pool_size(&wordpool);
for (i=len; i>=0; i--) {
find_subwords(&wordpool, i, print_word_and_remainder);
}
}
Dict_close(dict);
return 0;
}