/home/toolbox/public_html/solutions/6/642/j.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <stdint.h>
7 #include <math.h>
8 #include <stdlib.h>
9
10 #define TRUE (1 == 1)
11 #define FALSE (1 != 1)
12
13 #define DEBUG if (FALSE)
14
15 #define MAX_DICTIONARY_WORDS 101
16 #define MAX_LENGTH 8
17
18 /*
19 * Author: Isaac Traxler
20 * Date: 2013-02-08
21 * Purpose: fun
22 * Problem: 642 - Word Amalgamation
23 */
24
25 /*
26 * This template reads data until a terminating value is reached.
27 */
28
29 typedef struct
30 {
31 char word[MAX_LENGTH];
32 int hash;
33 }
34 wordSTRUCT;
35
36
37
38 wordSTRUCT dict[MAX_DICTIONARY_WORDS];
39 int dictCount;
40 wordSTRUCT current;
41
42
43 int compChar(const char * a, const char * b)
44 {
45 /* FUNCTION compare function for qsort */
46 int toReturn = 0;
47
48 if (*a < *b)
49 toReturn = -1;
50 else if (*a > *b)
51 toReturn = 1;
52 return toReturn;
53 } /* FUNCTION compare function for qsort */
54
55 int hashWord(char wrd[])
56 {
57 /* FUNCTION hashWord */
58 int tot = 0;
59 int i;
60 char twrd[MAX_LENGTH];
61
62 strcpy(twrd, wrd);
63 qsort(twrd, strlen(twrd), 1, compChar);
64 DEBUG printf("word=[%s] sorted=[%s]\n", wrd, twrd);
65 for (i=0; strlen(twrd) > i; i++)
66 {
67 /* for each character */
68 tot = tot * 26 + twrd[i] - 'a' + 1;
69 } /* for each character */
70 return tot;
71 } /* FUNCTION hashWord */
72
73 int compStruct(wordSTRUCT *a, wordSTRUCT *b)
74 {
75 /* FUNCTION compStruct */
76 return strcmp(a->word, b->word);
77 } /* FUNCTION compStruct */
78
79 void dump()
80 {
81 /* FUNCTION dump */
82 int i;
83
84 printf("Dictionary Count = %d\n", dictCount);
85 for (i=0; i<dictCount; i++)
86 {
87 /* for */
88 printf(" %2d - %s (%d/%X)\n", i, dict[i].word, dict[i].hash, dict[i].hash);
89 } /* for */
90 printf("\n");
91 } /* FUNCTION dump */
92
93 void init()
94 {
95 /* FUNCTION init */
96
97 dictCount = 0;
98 scanf(" %s ", dict[dictCount].word);
99 while ('X' != dict[dictCount].word[0])
100 {
101 /* while */
102 dict[dictCount].hash = hashWord(dict[dictCount].word);
103 DEBUG printf("init: cnt=%d word=%s hash=%d/%X\n", dictCount, dict[dictCount].word, dict[dictCount].hash, dict[dictCount].hash);
104 dictCount++;
105 scanf(" %s ", dict[dictCount].word);
106 } /* while */
107 DEBUG dump();
108 /* sort dictionary array */
109 qsort(dict, dictCount, sizeof(wordSTRUCT), compStruct);
110 DEBUG dump();
111 } /* FUNCTION init */
112
113 int getInput()
114 {
115 /* FUNCTION getInput */
116 int dataReadFlag;
117
118 scanf(" %s ", current.word);
119 if ('X' != current.word[0])
120 {
121 /* another word to process */
122 dataReadFlag = TRUE;
123 current.hash = hashWord(current.word);
124 } /* another word to process */
125 else
126 {
127 /* all done */
128 dataReadFlag = FALSE;
129 } /* all done */
130 return (dataReadFlag);
131 } /* FUNCTION getInput */
132
133 void process()
134 {
135 /* FUNCTION process */
136 int matchFound = FALSE;
137 int i;
138
139 for (i=0; i < dictCount; i++)
140 {
141 /* for - check each word in the dictionary */
142 if (dict[i].hash == current.hash)
143 {
144 /* found a match */
145 matchFound = TRUE;
146 printf("%s\n", dict[i].word);
147 } /* found a match */
148 } /* for - check each word in the dictionary */
149 if (! matchFound)
150 {
151 printf("NOT A VALID WORD\n");
152 }
153 printf("******\n");
154 DEBUG printf("word [%s] = %d (%X)\n", current.word, current.hash, current.hash);
155 } /* FUNCTION process */
156
157 int main ()
158 {
159 /* main */
160 int moreToDo;
161
162 init();
163 moreToDo = getInput();
164 while (moreToDo)
165 {
166 /* while */
167 process();
168 moreToDo = getInput();
169 } /* while */
170
171 return EXIT_SUCCESS;
172 } /* main */
173