chunkyguy

Billboards: Facebook Hacker Cup 2012

Jan 24th, 2012
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.18 KB | None | 0 0
  1. //
  2. //  main.c
  3. //  Billboard
  4. //
  5. //  Created by Sidharth Juyal on 21/01/12.
  6. //  Copyright (c) 2012 whackylabs. All rights reserved.
  7. //
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #define T_MAX 1000
  13. #define DEBUG_OUT 0
  14.  
  15. void process(int caseno, int w, int h, size_t *wordlens,int wc);
  16. int getlines(size_t cpl, size_t *wordlens, int wc);
  17.  
  18. int main (int argc, const char * argv[]){
  19.     int T, W, H;
  20.     int ch, wc;
  21.     char word[T_MAX];
  22.     char *wptr = word; 
  23.     size_t wordlens[T_MAX];
  24.     freopen(argv[1], "r", stdin);
  25.    
  26.     T = -1;
  27.     while((ch = getchar())){
  28.         if(ch == '\n'){
  29.             if(T < 0){
  30.                 T = 1;
  31.             }else{
  32.                 *wptr = '\0';
  33.                 wordlens[wc++] = strlen(word);
  34.                 process(T, W, H, wordlens, wc);
  35.                 T++;
  36.             }
  37.             wc = 0;
  38.             W = H = -1;
  39.             wptr = word;
  40.         }else if(ch == ' '){
  41.             *wptr = '\0';
  42.             if(W < 0)
  43.                 W = atoi(word);
  44.             else if(H < 0)
  45.                 H = atoi(word);
  46.             else
  47.                 wordlens[wc++] = strlen(word);
  48.                
  49.             wptr = word;
  50.         }else if(ch == EOF){
  51.             *wptr = '\0';
  52.             wordlens[wc++] = strlen(word);
  53.             process(T, W, H, wordlens,wc);
  54.             break;
  55.         }else{
  56.             *wptr++ = ch;
  57.         }
  58.     }
  59.    
  60.     return 0;
  61. }
  62.  
  63. void process(int caseno, int w, int h, size_t *wordlens,int wc){
  64.     size_t  font;
  65.     size_t cpl;         //chars per line
  66.     int i;
  67.    
  68.     cpl = wordlens[0];
  69.     for(i = 1; i < wc; i++){
  70.         if(wordlens[i] > cpl)
  71.             cpl = wordlens[i];
  72.     }
  73.    
  74.     font = w/cpl;
  75.     if(DEBUG_OUT) printf("font: %lu cpl: %lu\n",font,cpl);
  76.     while(getlines(cpl, wordlens, wc)*font > h){
  77.         font--;
  78.         cpl = w/font;
  79.         if(DEBUG_OUT) printf("font: %lu cpl: %lu\n",font,cpl);
  80.     }
  81.    
  82.     printf("Case #%d: %lu\n",caseno, font);
  83.     if(DEBUG_OUT) printf("{%d, %d} %d\n\n",w, h, wc);
  84. }
  85.  
  86. int getlines(size_t cpl, size_t *wordlens, int wc){
  87.     int i;
  88.     int lines = 1;
  89.     size_t used_chars = 0;
  90.    
  91.     for(i = 0; i < wc;){
  92.         used_chars += wordlens[i];
  93.         if(used_chars <  cpl){
  94.             if(DEBUG_OUT) printf("[%lu]_",wordlens[i]);
  95.             i++;
  96.             used_chars++;
  97.         }else if(used_chars == cpl){
  98.             if(DEBUG_OUT) printf("[%lu]\n",wordlens[i]);           
  99.             lines++;
  100.             used_chars = 0;
  101.             i++;
  102.         }else{
  103.             if(DEBUG_OUT) printf("\n");
  104.             lines++;
  105.             used_chars = 0;
  106.         }
  107.     }
  108.     if(DEBUG_OUT)printf("\nlines: %d\n",lines);
  109.     return lines;
  110. }
Add Comment
Please, Sign In to add comment