Computer Programming Contest Preparation

ToolBox - Source for: 3/306/b.c



/home/toolbox/public_html/solutions/3/306/b.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 <stdlib.h>
    7 #include <math.h>
    8 #include <stdint.h>
    9 #include <ctype.h>
   10 
   11 #define TRUE  (1 == 1)
   12 #define FALSE (1 != 1)
   13 
   14 #define DEBUG if (FALSE)
   15 
   16 /* fprintf(stderr, "functionName: message", varslist); */
   17 
   18 /*
   19  *  Author: Isaac Traxler
   20  *    Date: 2022-04-22
   21  * Purpose: fun
   22  * Problem: 306 - Cipher
   23  */
   24 
   25 /*
   26  * This template reads data a specified number of times.
   27  */
   28 
   29 #define MAX_LINE 222
   30 #define BLANK ' '
   31 
   32 int blockLength;
   33 char line[MAX_LINE+1];
   34 char cpy[MAX_LINE+1];
   35 int k;
   36 int indicies[MAX_LINE];
   37 
   38 void init()
   39 {
   40     /* FUNCTION init */
   41     int i;
   42 
   43     scanf("%d ", &blockLength);
   44     DEBUG printf("blockLength: %d\n", blockLength);
   45     for (i=0; blockLength > i; i++)
   46         {
   47             /* get each index */
   48             scanf(" %d ", &indicies[i]);
   49             /* convert to 0 based indexing */
   50             indicies[i] = indicies[i] - 1;
   51             DEBUG printf("%d: %d\n", i, indicies[i]);
   52         } /* get each index */
   53 } /* FUNCTION init */
   54 
   55 void dump()
   56 {
   57     /* FUNCTION dump */
   58 } /* FUNCTION dump */
   59 
   60 void getInput()
   61 {
   62     /* FUNCTION getInput */
   63     int tmp;
   64     int skip = 1;
   65     int i;
   66     int j;
   67 
   68     fgets(line, MAX_LINE, stdin);
   69     sscanf(line, "%d ", &k);
   70     while (BLANK != line[skip])
   71         {
   72             skip++;
   73         }
   74     tmp = strlen(line) - 1;
   75     for (i=skip+1, j=0; i<tmp; i++, j++)
   76         {
   77             /* shift buffer down */
   78             line[j] = line[i];
   79         } /* shift buffer down */
   80     while (j < blockLength)
   81         {
   82             /* while - append spaces to make length k */
   83             line[j] = BLANK;
   84             j++;
   85         } /* while - append spaces to make length k */
   86     line[j] = 0;
   87 } /* FUNCTION getInput */
   88 
   89 void process()
   90 {
   91     /* FUNCTION process */
   92     int i;
   93     int j;
   94 
   95     DEBUG printf("(k %d) {line [%s])\n", k, line);
   96     for (i=0; k>i; ++i)
   97         {
   98             /* for each encoding */
   99             for (j=0; j<blockLength; j++)
  100                 {
  101                     /* clone line */
  102                     cpy[j] = line[j];
  103                 } /* clone line */
  104             for (j=0; j<blockLength; j++)
  105                 {
  106                     /* for each item to be rotated */
  107                     line[indicies[j]] = cpy[j];
  108                 } /* for each item to be rotated */
  109         } /* for each encoding */
  110     printf("%s\n", line);
  111 } /* FUNCTION process */
  112 
  113 int main()
  114 {
  115     /* main */
  116     int i;
  117 
  118     init();
  119     while (0 < blockLength)
  120         {
  121             /* while */
  122             getInput();
  123             while (0 < k)
  124                 {
  125                     /* do each message of the block */
  126                     process();
  127                     getInput();
  128                 } /* do each message of the block */
  129             printf("\n");
  130             init();
  131         } /* while */
  132 
  133     return EXIT_SUCCESS;
  134 } /* main */
  135 
  136