-
Notifications
You must be signed in to change notification settings - Fork 0
/
countdown.c
115 lines (97 loc) · 2.93 KB
/
countdown.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
/***************************************************************************
* Find longest sub-word(s) from set of letters
***************************************************************************/
#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);
}
}
static int found;
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');
found = TRUE;
}
int main(int argc, char **argv)
{
Pool wordpool;
int len, i;
int found1st;
if (argc != 2) {
fprintf(stderr, "Usage: %s letters\n", argv[0]);
exit(2);
}
dict = Dict_open(NULL);
len = make_pool(argv[1], &wordpool);
found = found1st = FALSE;
for (i=len; i>=0 && !(found && found1st); i--) {
if (found) {found1st=TRUE; found=FALSE;}
find_subwords(&wordpool, i, print_word_and_remainder);
}
Dict_close(dict);
return 0;
}