Computer Programming Contest Preparation

ToolBox - Source for: 1/146/a.c



/home/toolbox/public_html/solutions/1/146/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 /*
   16  *  Author: Isaac Traxler
   17  *    Date: 2015-03-10
   18  * Purpose:
   19  * Problem: 146
   20  */
   21 
   22 /*
   23  * This template reads data until a terminating value is reached.
   24  */
   25 
   26 #define MAX_SIZE 55
   27 char line[MAX_SIZE];
   28 int len;
   29 
   30 void init()
   31 {
   32     /* FUNCTION init */
   33 } /* FUNCTION init */
   34 
   35 void dump()
   36 {
   37     /* FUNCTION dump */
   38 } /* FUNCTION dump */
   39 
   40 int compAscend(const char * a, const char * b)
   41 {
   42     /* FUNCTION compare function for qsort */
   43     int toReturn = 0;
   44 
   45     if (*a < *b)
   46         toReturn = -1;
   47     else if (*a > *b)
   48         toReturn = 1;
   49     return toReturn;
   50 } /* FUNCTION compare function for qsort */
   51 
   52 int compDescend(const char * a, const char * b)
   53 {
   54     /* FUNCTION compare function for qsort */
   55     int toReturn = 0;
   56 
   57     if (*a < *b)
   58         toReturn = 1;
   59     else if (*a > *b)
   60         toReturn = -1;
   61     return toReturn;
   62 } /* FUNCTION compare function for qsort */
   63 
   64 int getInput()
   65 {
   66     /* FUNCTION getInput */
   67     int dataReadFlag;
   68 
   69     scanf("%s ", line);
   70 
   71     dataReadFlag = (0 != strcmp("#", line));
   72     return (dataReadFlag);
   73 } /* FUNCTION getInput */
   74 
   75 int findPlace(char line[])
   76 {
   77     /* FUNCTION findPlace */
   78     int i;
   79     int toReturn;
   80 
   81     for (i=strlen(line)-1; i>0; i--)
   82         {
   83             /* compare */
   84             if (line[i] > line[i-1])
   85                 {
   86                     /* found it */
   87                     toReturn = i-1;
   88                     i=0;
   89                 } /* found it */
   90         } /* compare */
   91     return toReturn;
   92 } /* FUNCTION findPlace */
   93 
   94 void swapSmallest(char line[])
   95 {
   96     /* FUNCTION swapSmallest */
   97     /* goal of this function is to find the smallest value
   98        greater than the first character and swap it with
   99        the first character */
  100     int i;
  101     int s = 0;
  102     char best = 'z';
  103 
  104     DEBUG printf("swap [%s]\n", line);
  105     for (i=1; i<strlen(line); i++)
  106         {
  107             /* for */
  108             DEBUG printf("line[%d] = '%c'  best = '%c'\n", i, line[i], best);
  109             if ((line[0] < line[i]) && (best > line[i]))
  110                 {
  111                     /* new best found */
  112                     s = i;
  113                     best = line[s];
  114                 } /* new best found */
  115         } /* for */
  116     line[s] = line[0];
  117     line[0] = best;
  118     DEBUG printf("swap exit: [%s]\n", line);
  119 } /* FUNCTION swapSmallest */
  120 
  121 void process()
  122 {
  123     /* FUNCTION process */
  124     char tmp[MAX_SIZE];
  125     int i;
  126     char t;
  127 
  128     strcpy(tmp, line);
  129     qsort(tmp, strlen(tmp), sizeof(char), compDescend);
  130     if (0 == strcmp(line,tmp))
  131         {
  132             /* already last permutation */
  133             printf("No Successor\n");
  134         } /* already last permutation */
  135     else
  136         {
  137             /* find successor */
  138             DEBUG printf("%s\n", line);
  139             DEBUG printf("%s ", line);
  140             i = findPlace(line); /* find the place where a swap can happen */
  141             swapSmallest(&line[i]);
  142             DEBUG printf("%s ", line);
  143             qsort(&line[i+1], (strlen(line)-(i+1)), sizeof(char), compAscend);
  144             DEBUG printf("%s\n", line);
  145             DEBUG printf("[%s] %d\n", &line[i+1], (strlen(line)-(i+1)));
  146             printf("%s\n", line);
  147         } /* find successor */
  148 } /* FUNCTION process */
  149 
  150 int main()
  151 {
  152     /* main */
  153     int moreToDo;
  154 
  155     init();
  156     moreToDo = getInput();
  157     while (moreToDo)
  158         {
  159             /* while */
  160             process();
  161             moreToDo = getInput();
  162         } /* while */
  163 
  164     return EXIT_SUCCESS;
  165 } /* main */
  166