Computer Programming Contest Preparation

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



/home/toolbox/public_html/solutions/6/628/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_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 int RL;
   36 int r;
   37 int d;
   38 
   39 
   40 void init()
   41 {
   42     /* FUNCTION init */
   43 } /* FUNCTION init */
   44 
   45 void dump()
   46 {
   47     /* FUNCTION dump */
   48     int i;
   49 
   50     printf("Words = %d\n", dictionaryCount);
   51     for (i=0; i<dictionaryCount; i++)
   52         {
   53             /* for */
   54             printf("%3d: [%s]\n", i, dictionary[i]);
   55         } /* for */
   56     printf("Rules = %d\n", ruleCount);
   57     for (i=0; i<ruleCount; i++)
   58         {
   59             /* for */
   60             printf("%3d: [%s]\n", i, rules[i]);
   61         } /* for */
   62     printf("\n");
   63 } /* FUNCTION dump */
   64 
   65 int getInput()
   66 {
   67     /* FUNCTION getInput */
   68     int dataReadFlag;
   69     int i;
   70     int t;
   71 
   72     dataReadFlag = 0 < scanf(" %d ", &dictionaryCount);
   73     if (dataReadFlag)
   74         {
   75             /* read rest of data set */
   76             for (i=0; i<dictionaryCount; i++)
   77                 {
   78                     /* for */
   79                     fgets(dictionary[i], MAX_WORD_LENGTH-1, stdin);
   80                     dictionary[i][strlen(dictionary[i]) - 1] = '\0';
   81                 } /* for */
   82             scanf(" %d ", &ruleCount);
   83             for (i=0; i<ruleCount; i++)
   84                 {
   85                     /* for */
   86                     fgets(rules[i], MAX_RULE_LENGTH-1, stdin);
   87                     rules[i][strlen(rules[i]) - 1] = '\0';
   88                 } /* for */
   89 
   90         } /* read rest of data set */
   91     return (dataReadFlag);
   92 } /* FUNCTION getInput */
   93 
   94 void doNums()
   95 {
   96     /* FUNCTION doNums */
   97     int nums = 0;
   98     int i1, i2,i3,i4, i5, i6, i7;
   99     char buff[99999];
  100     int b = 0;
  101 
  102 
  103     for (i1=0; i1<strlen(rules[r]); i1++)
  104         {
  105             /* for - count digits */
  106             if ('#' ==rules[r][i1])
  107                 {
  108                     /* found a # -- print word */
  109                     sprintf(&buff[b], "%s", dictionary[d]);
  110                     b = b + strlen(dictionary[d]);
  111                 } /* found a # -- print word */
  112             else
  113                 {
  114                     /* found a 0 */
  115                     nums++;
  116                     buff[b++] = '%';
  117                     buff[b++] = 'd';
  118                 }/* found a 0 */
  119         } /* for - count digits */
  120     buff[b++] = '\n';
  121     buff[b] = '\0';
  122     DEBUG printf("nums=%d\n", nums);
  123     DEBUG printf("buff=[%s] rules[%d]=[%s]\n", buff, r, rules[r]);
  124 
  125     switch (nums)
  126         {
  127         /* switch */
  128         case 7:
  129             for (i1=0; i1<10; i1++)
  130                 for (i2=0; i2<10; i2++)
  131                     for (i3=0; i3<10; i3++)
  132                         for (i4=0; i4<10; i4++)
  133                             for (i5=0; i5<10; i5++)
  134                                 for (i6=0; i6<10; i6++)
  135                                     for (i7=0; i7<10; i7++)
  136                                         {
  137                                             /* okay in nested loops */
  138                                             printf(buff, i1, i2, i3, i4, i5, i6, i7);
  139                                         } /* okay in nested loops */
  140             break;
  141         case 6:
  142             for (i1=0; i1<10; i1++)
  143                 for (i2=0; i2<10; i2++)
  144                     for (i3=0; i3<10; i3++)
  145                         for (i4=0; i4<10; i4++)
  146                             for (i5=0; i5<10; i5++)
  147                                 for (i6=0; i6<10; i6++)
  148                                     {
  149                                         /* okay in nested loops */
  150                                         printf(buff, i1, i2, i3, i4, i5, i6);
  151                                     } /* okay in nested loops */
  152             break;
  153         case 5:
  154             for (i1=0; i1<10; i1++)
  155                 for (i2=0; i2<10; i2++)
  156                     for (i3=0; i3<10; i3++)
  157                         for (i4=0; i4<10; i4++)
  158                             for (i5=0; i5<10; i5++)
  159                                 {
  160                                     /* okay in nested loops */
  161                                     printf(buff, i1, i2, i3, i4, i5);
  162                                 } /* okay in nested loops */
  163             break;
  164         case 4:
  165             for (i1=0; i1<10; i1++)
  166                 for (i2=0; i2<10; i2++)
  167                     for (i3=0; i3<10; i3++)
  168                         for (i4=0; i4<10; i4++)
  169                             {
  170                                 /* okay in nested loops */
  171                                 printf(buff, i1, i2, i3, i4);
  172                             } /* okay in nested loops */
  173             break;
  174         case 3:
  175             for (i1=0; i1<10; i1++)
  176                 for (i2=0; i2<10; i2++)
  177                     for (i3=0; i3<10; i3++)
  178                         {
  179                             /* okay in nested loops */
  180                             printf(buff, i1, i2, i3);
  181                         } /* okay in nested loops */
  182             break;
  183         case 2:
  184             for (i1=0; i1<10; i1++)
  185                 for (i2=0; i2<10; i2++)
  186                     {
  187                         /* okay in nested loops */
  188                         printf(buff, i1, i2);
  189                     } /* okay in nested loops */
  190             break;
  191         case 1:
  192             for (i1=0; i1<10; i1++)
  193                 {
  194                     /* okay in nested loops */
  195                     printf(buff, i1);
  196                 } /* okay in nested loops */
  197             break;
  198         } /* switch */
  199 } /* FUNCTION doNums */
  200 
  201 void process()
  202 {
  203     /* FUNCTION process */
  204 
  205     DEBUG dump();
  206 
  207     printf("--\n");
  208     for (r=0; r<ruleCount; r++)
  209         {
  210             /* for each rule */
  211             for (d=0; d<dictionaryCount; d++)
  212                 {
  213                     /* for each dictionary word */
  214                     doNums();
  215                 } /* for each dictionary word */
  216         } /* for each rule */
  217 } /* FUNCTION process */
  218 
  219 int main ()
  220 {
  221     /* main */
  222     int moreToDo;
  223 
  224     init();
  225     moreToDo = getInput();
  226     while (moreToDo)
  227         {
  228             /* while */
  229             process();
  230             moreToDo = getInput();
  231         } /* while */
  232 
  233     return EXIT_SUCCESS;
  234 } /* main */
  235