Computer Programming Contest Preparation

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



/home/toolbox/public_html/solutions/3/306/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 <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     if (0 < k)
   71         {
   72             /* another case */
   73             while (BLANK != line[skip])
   74                 {
   75                     skip++;
   76                 }
   77             tmp = strlen(line) - 1;
   78             for (i=skip+1, j=0; i<tmp; i++, j++)
   79                 {
   80                     /* shift buffer down */
   81                     line[j] = line[i];
   82                 } /* shift buffer down */
   83             while (j < blockLength)
   84                 {
   85                     /* while - append spaces to make length k */
   86                     line[j] = BLANK;
   87                     j++;
   88                 } /* while - append spaces to make length k */
   89             line[j] = 0;
   90         } /* another case */
   91 } /* FUNCTION getInput */
   92 
   93 void process()
   94 {
   95     /* FUNCTION process */
   96     int i;
   97     int j;
   98 
   99     DEBUG printf("(k %d) {line [%s])\n", k, line);
  100     if (k > (blockLength * blockLength))
  101         {
  102             /* every blockLenght the sequence repeats, so eliminate extra iterations */
  103             k = (k % (blockLength)) ;
  104         } /* every blockLenght the sequence repeats, so eliminate extra iterations */
  105     for (i=0; k>i; ++i)
  106         {
  107             /* for each encoding */
  108             for (j=0; j<blockLength; j++)
  109                 {
  110                     /* clone line */
  111                     cpy[j] = line[j];
  112                 } /* clone line */
  113             for (j=0; j<blockLength; j++)
  114                 {
  115                     /* for each item to be rotated */
  116                     line[indicies[j]] = cpy[j];
  117                 } /* for each item to be rotated */
  118         } /* for each encoding */
  119     printf("%s\n", line);
  120 } /* FUNCTION process */
  121 
  122 int main()
  123 {
  124     /* main */
  125     int i;
  126 
  127     init();
  128     while (0 < blockLength)
  129         {
  130             /* while */
  131             getInput();
  132             while (0 < k)
  133                 {
  134                     /* do each message of the block */
  135                     process();
  136                     getInput();
  137                 } /* do each message of the block */
  138             printf("\n");
  139             init();
  140         } /* while */
  141 
  142     return EXIT_SUCCESS;
  143 } /* main */
  144 
  145