Computer Programming Contest Preparation

ToolBox - Source for: 6/642/a.c



/home/toolbox/public_html/solutions/6/642/a.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 char dict[MAX_DICTIONARY_WORDS][MAX_LENGTH];
   30 int  hash[MAX_DICTIONARY_WORDS];
   31 int  dictCount;
   32 char word[MAX_LENGTH];
   33 int wordHash;
   34 
   35 int compChar(const char * a, const char * b)
   36 {
   37     /* FUNCTION compare function for qsort */
   38     int toReturn = 0;
   39 
   40     if (*a < *b)
   41         toReturn = -1;
   42     else if (*a > *b)
   43         toReturn = 1;
   44     return toReturn;
   45 } /* FUNCTION compare function for qsort */
   46 
   47 int hashWord(char wrd[])
   48 {
   49     /* FUNCTION hashWord */
   50     int tot = 0;
   51     int i;
   52     char twrd[MAX_LENGTH];
   53 
   54     strcpy(twrd, wrd);
   55     qsort(twrd, strlen(twrd), 1, compChar);
   56     DEBUG printf("word=[%s] sorted=[%s]\n", wrd, twrd);
   57     for (i=0; strlen(twrd) > i; i++)
   58         {
   59             /* for each character  */
   60             tot = tot * 26 + twrd[i] - 'a' + 1;
   61         } /* for each character  */
   62     return tot;
   63 } /* FUNCTION hashWord */
   64 
   65 static int compString(const void *a, const void *b)
   66 {
   67     /* FUNCTION compString */
   68     return (strcmp(*(char **)a, *(char **)b));
   69 } /* FUNCTION compString */
   70 
   71 void dump()
   72 {
   73     /* FUNCTION dump */
   74     int i;
   75 
   76     printf("Dictionary Count = %d\n", dictCount);
   77     for (i=0; i<dictCount; i++)
   78         {
   79             /* for */
   80             printf("  %2d - %s\n", i, dict[i]);
   81         } /* for */
   82 } /* FUNCTION dump */
   83 
   84 void init()
   85 {
   86     /* FUNCTION init */
   87 
   88     dictCount = 0;
   89     scanf(" %s ", dict[dictCount]);
   90     while ('X' != dict[dictCount][0])
   91         {
   92             /* while */
   93             hash[dictCount] = hashWord(dict[dictCount]);
   94             dictCount++;
   95             scanf(" %s ", dict[dictCount]);
   96         } /* while */
   97     dump();
   98     qsort(dict, dictCount, sizeof(char *), compString);
   99     dump();
  100 } /* FUNCTION init */
  101 
  102 int getInput()
  103 {
  104     /* FUNCTION getInput */
  105     int dataReadFlag;
  106 
  107     scanf(" %s ", word);
  108     if ('X' != word[0])
  109         {
  110             /* another word to process */
  111             dataReadFlag = TRUE;
  112             wordHash = hashWord(word);
  113         } /* another word to process */
  114     else
  115         {
  116             /* all done */
  117             dataReadFlag = FALSE;
  118         } /* all done */
  119     return (dataReadFlag);
  120 } /* FUNCTION getInput */
  121 
  122 void process()
  123 {
  124     /* FUNCTION process */
  125     int matchFound = FALSE;
  126     int i;
  127 
  128     for (i=0; i < dictCount; i++)
  129         {
  130             /* for - check earch word in the dictionary */
  131             if (wordHash == hash[i])
  132                 {
  133                     /* found a match */
  134                     matchFound = TRUE;
  135                     printf("%s\n", dict[i]);
  136                 } /* found a match */
  137         } /* for - check earch word in the dictionary */
  138     if (! matchFound)
  139         {
  140             printf("NOT A VALID WORD\n");
  141         }
  142     printf("******\n");
  143     DEBUG printf("word [%s] = %d (%X)\n", word, wordHash, wordHash);
  144 } /* FUNCTION process */
  145 
  146 int main ()
  147 {
  148     /* main */
  149     int moreToDo;
  150 
  151     init();
  152     moreToDo = getInput();
  153     while (moreToDo)
  154         {
  155             /* while */
  156             process();
  157             moreToDo = getInput();
  158         } /* while */
  159 
  160     return EXIT_SUCCESS;
  161 } /* main */
  162