-
Notifications
You must be signed in to change notification settings - Fork 2
/
wsolver.c
296 lines (255 loc) · 6.06 KB
/
wsolver.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "tree.h"
#define ASSERT(cond) {if (!(cond)) (*((char *)0) = 0);}
#define WORD_DB "/usr/share/dict/words"
#define MAX_WORD_SIZE 80
struct list {
char *word;
struct list *next;
};
struct word_node {
char *word;
RB_ENTRY(word_node) rb_node;
};
typedef struct word_node wnode_t;
typedef struct tree_handle {
RB_HEAD(word_tree, word_node) th_tree;
} tree_handle_t;
/* Globals */
char copy[MAX_WORD_SIZE];
tree_handle_t th;
struct list *printed_wlist_head = NULL;
int
str_compare(const void *query_key, const void *cur)
{
char *x = ((wnode_t *)query_key)->word;
char *y = ((wnode_t *)cur)->word;
if (strcmp(x, y) < 0) {
return (-1);
} else if (strcmp(x, y) > 0) {
return (1);
} else {
return (0);
}
}
RB_PROTOTYPE(word_tree, word_node, rb_node, str_compare);
RB_GENERATE(word_tree, word_node, rb_node, str_compare);
wnode_t *get_tree_node(char *str)
{
wnode_t *w = ((wnode_t *) malloc(sizeof(wnode_t)));
if (w) {
w->word = malloc(strlen(str));
if (w->word) {
strcpy(w->word, str);
} else {
perror("malloc");
exit(1);
}
} else {
perror("malloc");
exit(1);
}
return (w);
}
void
init_tree(tree_handle_t *handle)
{
RB_INIT(&handle->th_tree);
}
int
add_word_to_tree(tree_handle_t *handle, char *add_str)
{
wnode_t *node;
int ret = 0;
ASSERT(add_str != NULL);
node = get_tree_node(add_str);
if (RB_FIND(word_tree, &handle->th_tree, node) != NULL) {
/* Node already present */
free(node);
ret = EEXIST;
} else {
RB_INSERT(word_tree, &handle->th_tree, (void *)node);
}
return (ret);
}
int
search_word_in_tree(tree_handle_t *handle, char *search_str)
{
wnode_t temp, *node;
int ret;
memset((void *)&temp, 0, sizeof(wnode_t));
temp.word = search_str;
if ((node = RB_FIND(word_tree, &handle->th_tree, &temp)) != NULL) {
/* Found */
ret = 0;
} else {
/* Not Found */
ret = ENOENT;
}
return (ret);
}
int
populate_tree(tree_handle_t *tree)
{
/* Assumption : no word in the WORD_DB is >= MAX_WORD_SIZE characters long */
FILE *fp;
char temp[MAX_WORD_SIZE];
fp = fopen(WORD_DB, "r");
if (fp == NULL) {
fprintf(stderr, "Could not open word database at : %s\n",
WORD_DB);
exit(1);
}
while(fscanf(fp, "%s", temp) != EOF) {
if (add_word_to_tree(tree, temp) == EEXIST) {
fprintf(stderr, "%s already in tree\n", temp);
}
}
fclose(fp);
return (0);
}
int
query_word_from_user(char *temp)
{
int i;
printf("Enter jumbled word: ");
fflush(stdin);
fgets(temp, MAX_WORD_SIZE, stdin);
/* fgets() reads the newline into the buffer. Remove if present */
for (i = 0; i < strlen(temp); i++) {
if (temp[i] == '\n') {
temp[i] = '\0';
}
}
}
void __attribute__((always_inline))
swap(char *a, char *b)
{
int t;
t = *a;
*a = *b;
*b = t;
}
/*
* The recursive algorithm below does the following:
* Lets take the letter combination - abc
*
* It starts off by producing all possible permutations starting with 'a'.
* This is done by keeping 'a' unchanged and calling get_all_permutations
* (recursively) with an advanced pointer (p+1). Note that the changes are being
* done in-place and hence the input string is getting modified with each swap.
* So, at the end of all iterations, get_all_permutations() makes sure that it
* returns the input string to its initial state.
*
* In the above example, the following is the call stack():
* We use the short form g_a_c() to represent get_all_permutations().
* g_a_c(abc, 3)
* g_a_c(bc, 2)
* g_a_c(c, 1) -> Print 'abc'
* i is 0 here, which is less than (len - 1) => (2 - 1)
* swap elem0 with elem1 => b with c. String becomes cb
* g_a_c(cb, 2)
* g_a_c(b, 1) -> Print 'acb'
* i is 1 here which is NOT less than (2 - 1). Time to restore the string to
* its original position
* Rotate the current string to the left once to get back to the initial
* state.
* String no becomes 'bc'.
*
* After all the permutations starting with 'a', it brings the next letter in
* the input string to the 0th place by swapping it with what was there
* already. In this case, 'b' is swapped with 'a'. Now, the string becomes
* 'bac'. It agains gets all permutations starting with 'b'. This way, it goes
* all the way until all the for the last letter is obtained.
*/
struct list *
get_wlist_node(char *str)
{
struct list *temp = malloc(sizeof(struct list));
if (temp) {
temp->word = malloc(strlen(str));
if (temp->word) {
strcpy(temp->word, str);
temp->next = NULL;
} else {
perror("malloc");
exit(1);
}
} else {
perror("malloc");
exit(1);
}
}
int
search_printed_words(char *str)
{
struct list *prev, *temp;
if (printed_wlist_head == NULL) {
printed_wlist_head = get_wlist_node(str);
return (0);
}
for (prev = temp = printed_wlist_head; temp; prev = temp,
temp = temp->next) {
if (strcmp(temp->word, str) == 0) {
/* Found */
return EEXIST;
}
}
/* New word. Add to list */
temp = get_wlist_node(str);
prev->next = temp;
return (0);
}
void
get_all_permutations(char *p, int len)
{
int i, t;
if (len == 1) {
if (search_word_in_tree(&th, copy) == 0) {
if (search_printed_words(copy) == 0) {
printf("%s\n", copy);
}
}
return;
}
for (i = 0; i < len; i++) {
get_all_permutations(p+1, len - 1);
/*
* Bring the next letter to position 0. get_all_permutations()
* starting with that letter.
*/
if (i < len - 1) {
swap(&p[0], &p[i+1]);
} else {
/*
* Indicates completion of all permutations. Time to
* restore the string to its initial state. If the
* initial string was "abcde", at this point, due to 4
* swaps, (not 5, since we are in the fifth iteration)
* the string would be - "eabcd". We restore initial
* condition by doing a rotate_left_once.
*/
for (i = 0; i < len - 1; i++) {
swap(&p[i], &p[i+1]);
}
}
}
}
int
main()
{
int ret;
char temp[MAX_WORD_SIZE];
init_tree(&th);
populate_tree(&th);
while(1) {
query_word_from_user(temp);
/* Using strcpy since the input is sanitized via fgets */
strcpy(copy, temp);
get_all_permutations(©[0], strlen(copy));
}
return (0);
}