Computer Programming Contest Preparation

ToolBox - Source for: 1/156/a.c



/home/toolbox/public_html/solutions/1/156/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 /*
   16  *  Author: Isaac Traxler
   17  *    Date:
   18  * Purpose: fun
   19  * Problem:
   20  */
   21 
   22 /*
   23  * This template reads data until a terminating value is reached.
   24  */
   25 
   26 #define MAX_WORDS 6001
   27 #define MAX_LENGTH 21
   28 
   29 
   30 typedef struct
   31 {
   32     /* struct REC_TYPE */
   33     char org[MAX_LENGTH];
   34     char srt[MAX_LENGTH];
   35     int lngth;
   36     int cnt;
   37 } /* struct REC_TYPE */ REC_TYPE;
   38 
   39 char wrd[MAX_LENGTH];
   40 char srt[MAX_LENGTH];
   41 REC_TYPE dict[MAX_WORDS];
   42 int idx = 1;
   43 
   44 void init()
   45 {
   46     /* FUNCTION init */
   47 } /* FUNCTION init */
   48 
   49 void dump()
   50 {
   51     /* FUNCTION dump */
   52 } /* FUNCTION dump */
   53 
   54 int compAscend(const char * a, const char * b)
   55 {
   56     /* FUNCTION compare function for qsort */
   57     int toReturn = 0;
   58 
   59     if (*a < *b)
   60         toReturn = -1;
   61     else if (*a > *b)
   62         toReturn = 1;
   63     return toReturn;
   64 } /* FUNCTION compare function for qsort */
   65 
   66 int compare(const void * a, const void * b)
   67 {
   68     /* FUNCTION compare */
   69     int i;
   70     REC_TYPE * A;
   71     REC_TYPE * B;
   72 
   73     A = (REC_TYPE *)a;
   74     B = (REC_TYPE *)b;
   75 
   76     i = strcmp(A->org, B->org);
   77     return i;
   78 } /* FUNCTION compare */
   79 
   80 
   81 void insertWord(int x, int tl)
   82 {
   83     /* FUNCTION insertWord */
   84     strcpy(dict[x].org, wrd);
   85     strcpy(dict[x].srt, srt);
   86     dict[x].lngth = tl;
   87     dict[x].cnt = 1;
   88 } /* FUNCTION insertWord */
   89 
   90 int findWord()
   91 {
   92     /* FUNCTION notFound */
   93     int i;
   94     int fnd = -1;
   95 
   96     for (i=0; idx>i; i++)
   97         {
   98             /* for */
   99             if (0 == strcmp(srt, dict[i].srt))
  100                 {
  101                     fnd = i;
  102                 }
  103         } /* for */
  104     return (fnd);
  105 } /* FUNCTION notFound */
  106 
  107 void getInput()
  108 {
  109     /* FUNCTION getInput */
  110     int tl;
  111     int i = 0;
  112     int tmp;
  113 
  114     /* put first word in */
  115     scanf(" %s ", wrd);
  116     tl = strlen(wrd);
  117     for (i=0; tl>i; i++)
  118         {
  119             /* foreach letter */
  120             srt[i] = tolower(wrd[i]);
  121         } /* foreach letter */
  122     srt[i] = 0;
  123     qsort(srt, tl, sizeof(char), compAscend);
  124     insertWord(0, tl);
  125     /* priming read */
  126     scanf(" %s ", wrd);
  127     while ('#' != wrd[0])
  128         {
  129             /* another word to process */
  130             tl = strlen(wrd);
  131             for (i=0; tl>i; i++)
  132                 {
  133                     /* foreach letter */
  134                     srt[i] = tolower(wrd[i]);
  135                 } /* foreach letter */
  136             srt[i] = 0;
  137             qsort(srt, tl, sizeof(char), compAscend);
  138             tmp = findWord();
  139             if (-1 == tmp)
  140                 {
  141                     /* new word */
  142                     DEBUG printf("I %s %s idx = %d\n", wrd, srt, idx);
  143                     insertWord(idx, tl);
  144                     idx++;
  145                 } /* new word */
  146             else
  147                 {
  148                     /* already here */
  149                     dict[tmp].cnt++;
  150                 } /* already here */
  151             scanf(" %s ", wrd);
  152         } /* another word to process */
  153 } /* FUNCTION getInput */
  154 
  155 void process()
  156 {
  157     /* FUNCTION process */
  158     int i;
  159 
  160     qsort(dict, idx, sizeof(REC_TYPE), compare);
  161     for (i=0; idx>i; i++)
  162         {
  163             /* for */
  164             DEBUG printf("dump: %d %s %s %d\n", i, dict[i].org, dict[i].srt, dict[i].cnt);
  165             if (1 == dict[i].cnt)
  166                 {
  167                     /* found singleton */
  168                     printf("%s\n", dict[i].org);
  169                 } /* found singleton */
  170         } /* for */
  171 } /* FUNCTION process */
  172 
  173 int main()
  174 {
  175     /* main */
  176     int moreToDo;
  177 
  178     getInput();
  179     process();
  180 
  181     return EXIT_SUCCESS;
  182 } /* main */
  183