Guest User

facebook hackercup billboard perl 2

a guest
Jan 27th, 2012
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.03 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use POSIX;
  5. use List::Util qw[min max];
  6.  
  7. open(INPUT, '<', 'input.txt'); open(OUTPUT, '>', 'output.txt');
  8.  
  9. my @lines = <INPUT>;
  10. for my $i(1 .. $#lines)
  11. {  
  12.     my @values  = split(' ',$lines[$i]);
  13.     my $w       = shift @values;
  14.     my $h       = shift @values;   
  15.     #my $fsize   = ceil(sqrt(($w*$h)/(length(join(' ',@values))))); #approximate font size
  16.     my $fsize = ceil($w/(max(map(length,@values)))); #approximate font size
  17.  
  18.     while($fsize > 0)
  19.     {      
  20.       my @words           = @values;
  21.       my $max_lines       = floor($h/$fsize);
  22.       my @lines;     
  23.       for my $i(1..$max_lines)
  24.       {
  25.         $lines[$i] = '';
  26.         while( @words )
  27.         {                  
  28.             if((((length $lines[$i])+(length $words[0]))*$fsize) <= $w )
  29.             {
  30.                 $lines[$i] .= (shift @words) . ' ';
  31.             }
  32.             else
  33.             {
  34.               last;
  35.             }            
  36.         }
  37.       }  
  38.       unless(@words)
  39.       {
  40.         last;  
  41.       }
  42.         $fsize--;
  43.     }
  44.   unless($i == 1){print OUTPUT "\n";}
  45.   print OUTPUT  "Case #$i: $fsize";
  46. }
  47. close(INPUT); close(OUTPUT);
Add Comment
Please, Sign In to add comment