Computer Programming Contest Preparation

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



/home/toolbox/public_html/solutions/102/10252/d.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     if (EOF == scanf("%s ", a))
   57         {
   58             /* end of file found */
   59             dataReadFlag = FALSE;
   60         } /* end of file found */
   61     else
   62         {
   63             /* read second string */
   64             scanf("%s ", b);
   65         } /* read second string */
   66     return (dataReadFlag);
   67 } /* FUNCTION getInput */
   68 
   69 void countOccurences(char *buf, int *cnt)
   70 {
   71     /* FUNCTION process */
   72     int i;
   73     int tmp;
   74 
   75     for (i=0; strlen(buf)>i; i++)
   76         {
   77             /* count occurrences */
   78             tmp = buf[i] - 'a';
   79             cnt[tmp] = cnt[tmp] + 1;
   80         } /* count occurrences */
   81 } /* FUNCTION process */
   82 
   83 void process()
   84 {
   85     /* FUNCTION process */
   86     int i;
   87     int j;
   88     int tmp;
   89 
   90     countOccurences(a, aCnt);
   91     countOccurences(b, bCnt);
   92 
   93     for (i=0; 26>i; i++)
   94         {
   95             /* compare count of each letter */
   96             tmp = ((aCnt[i] < bCnt[i]) ? aCnt[i] : bCnt[i]);
   97             for (j=0; tmp>j; j++)
   98                 {
   99                     /* print each letter */
  100                     printf("%c", 'a'+i);
  101                 } /* print each letter */
  102         } /* compare count of each letter */
  103     printf("\n");
  104 
  105 } /* FUNCTION process */
  106 
  107 int main ()
  108 {
  109     /* main */
  110     int moreToDo;
  111 
  112     moreToDo = getInput();
  113     while (moreToDo)
  114         {
  115             /* while */
  116             init();
  117             process();
  118             moreToDo = getInput();
  119         } /* while */
  120 
  121     return EXIT_SUCCESS;
  122 } /* main */
  123