Computer Programming Contest Preparation

ToolBox - Source for: 8/850/judged.c



/home/toolbox/public_html/solutions/8/850/judged.c
    1 #include <stdio.h>
    2 #include <stdlib.h>
    3 #include <string.h>
    4 #include <ctype.h>
    5 
    6 /*
    7  *  Author: Matthew Eastman <meastman@cct.lsu.edu>
    8  *    Date: 2006-10-03
    9  * Purpose: Contest practice
   10  * Problem: 850 - Crypt Kicker II <http://isaac.lsu.edu/udv/v8/850.html>
   11  */
   12 
   13 #define MAX_LINES 105
   14 #define MAX_LENGTH 85
   15 
   16 #define FALSE 0
   17 #define TRUE  1
   18 
   19 char big_buf[MAX_LINES][MAX_LENGTH];
   20 char small_buf[MAX_LENGTH];
   21 char plaintext[] = "the quick brown fox jumps over the lazy dog";
   22 char key[256];
   23 int num_lines;
   24 
   25 void getInput()
   26 {
   27     char *dangerous;
   28 
   29     num_lines = 0;
   30 
   31     while (TRUE)
   32         {
   33             if (NULL == fgets(big_buf[num_lines], sizeof(char) * MAX_LENGTH,
   34                               stdin))
   35                 {
   36                     break;
   37                 }
   38 
   39             dangerous = big_buf[num_lines];
   40             while (('\0' != *dangerous) && (!iscntrl(*dangerous)))
   41                 dangerous++;
   42             if (dangerous == big_buf[num_lines])
   43                 break;
   44 
   45             *dangerous = '\0';
   46             num_lines++;
   47         }
   48 }
   49 
   50 void solveProblem()
   51 {
   52     int i;
   53     int j;
   54 
   55     for (i = 0; i < num_lines; i++)
   56         {
   57             memset(key, 0, sizeof(key));
   58             for (j = 0; big_buf[i][j] && plaintext[j]; j++)
   59                 {
   60                     if ((big_buf[i][j] == ' ') && (plaintext[j] != ' '))
   61                         break;
   62                     else if ((big_buf[i][j] != ' ') && (plaintext[j] == ' '))
   63                         break;
   64                     else if ('\0' == key[big_buf[i][j]])
   65                         key[big_buf[i][j]] = plaintext[j];
   66                     else if (key[big_buf[i][j]] != plaintext[j])
   67                         break;
   68                 }
   69 
   70             if (!big_buf[i][j] && !plaintext[j])
   71                 break;
   72         }
   73 
   74     if (i >= num_lines)
   75         {
   76             printf("No solution.\n");
   77             return;
   78         }
   79 
   80     /* printf("key offset: %d\n", i); */
   81     for (i = 0; i < num_lines; i++)
   82         {
   83             for (j = 0; big_buf[i][j]; j++)
   84                 printf("%c", key[big_buf[i][j]]);
   85             printf("\n");
   86         }
   87 }
   88 
   89 int main()
   90 {
   91     int num_problems = 0;
   92     int i;
   93 
   94     fgets(small_buf, sizeof(small_buf), stdin);
   95     sscanf(small_buf, "%d", &num_problems);
   96     fgets(small_buf, sizeof(small_buf), stdin); /* eat a line */
   97 
   98     for (i = 0; i < num_problems; i++)
   99         {
  100             getInput();
  101             if (0 < i)
  102                 printf("\n");
  103             solveProblem();
  104         }
  105 
  106     return EXIT_SUCCESS;
  107 }
  108 
  109