Computer Programming Contest Preparation

ToolBox - Source for: 102/10252/a.c



/home/toolbox/public_html/solutions/102/10252/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 (TRUE)
   14 #define MAX_CHARS 1004
   15 
   16 /*
   17  *  Author: Josh Abadie and Isaac Traxler
   18  *    Date: 2008-04-28
   19  * Purpose: practice
   20  * Problem: 10252 - Common Permutation (page 69 of Programming Challenges)
   21  */
   22 
   23 /*
   24  * This template reads data until a terminating value is reached.
   25  */
   26 
   27 char a[MAX_CHARS];
   28 char b[MAX_CHARS];
   29 int aCnt[26];
   30 int bCnt[26];
   31 
   32 void init()
   33 {
   34     /* FUNCTION init */
   35     int i;
   36 
   37     for (i=0; 26>i; i++)
   38         {
   39             /* zero out each letter counter */
   40             aCnt[i] = 0;
   41             bCnt[i] = 0;
   42         } /* zero out each letter counter */
   43 } /* FUNCTION init */
   44 
   45 void dump()
   46 {
   47     /* FUNCTION dump */
   48     printf("\na=(%s)\nb=(%s)\n", a, b);
   49 } /* FUNCTION dump */
   50 
   51 int getInput()
   52 {
   53     /* FUNCTION getInput */
   54     int dataReadFlag = TRUE;
   55 
   56     fgets(a, MAX_CHARS, stdin);
   57     if (feof(stdin))
   58         dataReadFlag = FALSE;
   59     else
   60         {
   61             /* something to read */
   62             dataReadFlag = TRUE;
   63             a[strlen(a)-1] = 0;
   64             fgets(b, MAX_CHARS, stdin);
   65             b[strlen(b)-1] = 0;
   66         } /* something to read */
   67     return (dataReadFlag);
   68 } /* FUNCTION getInput */
   69 
   70 void countOccurences(char *buf, int *cnt)
   71 {
   72     /* FUNCTION process */
   73     int i;
   74     int tmp;
   75 
   76     for (i=0; strlen(buf)>i; i++)
   77         {
   78             /* count occurrences */
   79             tmp = buf[i] - 'a';
   80             cnt[tmp] = cnt[tmp] + 1;
   81         } /* count occurrences */
   82 } /* FUNCTION process */
   83 
   84 void process()
   85 {
   86     /* FUNCTION process */
   87     int i;
   88     int j;
   89     int tmp;
   90 
   91     countOccurences(a, aCnt);
   92     countOccurences(b, bCnt);
   93 
   94     for (i=0; 26>i; i++)
   95         {
   96             /* compare count of each letter */
   97             tmp = ((aCnt[i] < bCnt[i]) ? aCnt[i] : bCnt[i]);
   98             for (j=0; tmp>j; j++)
   99                 {
  100                     /* print each letter */
  101                     printf("%c", 'a'+i);
  102                 } /* print each letter */
  103         } /* compare count of each letter */
  104     printf("\n");
  105 
  106 } /* FUNCTION process */
  107 
  108 int main ()
  109 {
  110     /* main */
  111     int moreToDo;
  112 
  113     moreToDo = getInput();
  114     while (moreToDo)
  115         {
  116             /* while */
  117             init();
  118             process();
  119             moreToDo = getInput();
  120         } /* while */
  121 
  122     return EXIT_SUCCESS;
  123 } /* main */
  124