Computer Programming Contest Preparation

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



/home/toolbox/public_html/solutions/6/628/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_WORDS 102
   16 #define MAX_WORD_LENGTH 258
   17 #define MAX_RULES 1000
   18 #define MAX_RULE_LENGTH 258
   19 
   20 /*
   21  *  Author: Isaac Traxler
   22  *    Date: 2013-01-25
   23  * Purpose: fun
   24  * Problem: 628 - passwords
   25  */
   26 
   27 /*
   28  * This template reads data until a terminating value is reached.
   29  */
   30 
   31 int dictionaryCount;
   32 char dictionary[MAX_WORDS][MAX_WORD_LENGTH];
   33 int ruleCount;
   34 char rules[MAX_RULES][MAX_RULE_LENGTH];
   35 
   36 
   37 void init()
   38 {
   39     /* FUNCTION init */
   40 } /* FUNCTION init */
   41 
   42 void dump()
   43 {
   44     /* FUNCTION dump */
   45     int i;
   46 
   47     printf("Words = %d\n", dictionaryCount);
   48     for (i=0; i<dictionaryCount; i++)
   49         {
   50             /* for */
   51             printf("%3d: [%s]\n", i, dictionary[i]);
   52         } /* for */
   53     printf("Rules = %d\n", ruleCount);
   54     for (i=0; i<ruleCount; i++)
   55         {
   56             /* for */
   57             printf("%3d: [%s]\n", i, rules[i]);
   58         } /* for */
   59     printf("\n");
   60 } /* FUNCTION dump */
   61 
   62 int getInput()
   63 {
   64     /* FUNCTION getInput */
   65     int dataReadFlag;
   66     int i;
   67     int t;
   68 
   69     dataReadFlag = 0 < scanf(" %d ", &dictionaryCount);
   70     if (dataReadFlag)
   71         {
   72             /* read rest of data set */
   73             for (i=0; i<dictionaryCount; i++)
   74                 {
   75                     /* for */
   76                     fgets(dictionary[i], MAX_WORD_LENGTH-1, stdin);
   77                     dictionary[i][strlen(dictionary[i]) - 1] = '\0';
   78                 } /* for */
   79             scanf(" %d ", &ruleCount);
   80             for (i=0; i<ruleCount; i++)
   81                 {
   82                     /* for */
   83                     fgets(rules[i], MAX_RULE_LENGTH-1, stdin);
   84                     rules[i][strlen(rules[i]) - 1] = '\0';
   85                 } /* for */
   86 
   87         } /* read rest of data set */
   88     return (dataReadFlag);
   89 } /* FUNCTION getInput */
   90 
   91 void process()
   92 {
   93     /* FUNCTION process */
   94     int i;
   95     int r;
   96     int d;
   97     int t;
   98 
   99     DEBUG dump();
  100 
  101     printf("--\n");
  102     for (r=0; r<ruleCount; r++)
  103         {
  104             /* for each rule */
  105             for (i=0; i<dictionaryCount; i++)
  106                 {
  107                     /* for each dictionary word */
  108                     for (d=0; d<10; d++)
  109                         {
  110                             /* for each digit */
  111                             for (t=0; t<strlen(rules[r]); t++)
  112                                 {
  113                                     /* for each subrule */
  114                                     if ('#' == rules[r][t])
  115                                         {
  116                                             /* output word */
  117                                             printf("%s", dictionary[i]);
  118                                         } /* output word */
  119                                     else
  120                                         {
  121                                             /* output digit */
  122                                             printf("%d", d);
  123                                         } /* output digit */
  124                                 } /* for each subrule */
  125                             printf("\n");
  126                         } /* for each digit */
  127                 } /* for each dictionary word */
  128         } /* for each rule */
  129 } /* FUNCTION process */
  130 
  131 int main ()
  132 {
  133     /* main */
  134     int moreToDo;
  135 
  136     init();
  137     moreToDo = getInput();
  138     while (moreToDo)
  139         {
  140             /* while */
  141             process();
  142             moreToDo = getInput();
  143         } /* while */
  144 
  145     return EXIT_SUCCESS;
  146 } /* main */
  147