- 论坛徽章:
- 1
|
Google Code Jam 2011 资格赛有4道题,貌似几乎没有用perl解题的。试着用perl解了前两道题,写的不好,大家看看。。。
题目描述和数据文件在此http://code.google.com/codejam/contest/dashboard?c=975485#
第一题代码:- #!/usr/bin/perl -w
- use strict;
- use List::Util qw(max);
- open FIL, $ARGV[0];
- my $n = 1;
- while (<FIL>) {
- if ($_ =~ /[OB]/) {
- my $last_press = 0;
- my ($Olast, $Blast) = (1, 1);
- my ($Otime, $Btime) = (0, 0);
- while ($_ =~ /([OB])\s(\d+)/g) {
- if ($1 eq "O") {
- my $Odiff = abs($2 - $Olast);
- $Olast = $2;
- $Otime = $Otime + $Odiff;
- $Otime = max $Otime, $last_press;
- $Otime = $Otime + 1;
- $last_press = $Otime;
- }
- if ($1 eq "B") {
- my $Bdiff = abs($2 -$Blast);
- $Blast = $2;
- $Btime = $Btime + $Bdiff;
- $Btime = max $Btime, $last_press;
- $Btime = $Btime + 1;
- $last_press = $Btime;
- }
- }
- printf "Case #%d: %d\n", $n, $last_press;
- $n++;
- }
- }
- close FIL;
复制代码 第二题代码:- #!/usr/bin/perl -w
- use strict;
- open FIL, "$ARGV[0]";
- my $n = 1;
- while (<FIL>){
- if ($. != 1){
- my ($c, $d, $string);
- my @combin = ();
- my @oppose = ();
- my @string = ();
- my @line = split(' ', $_);
- $c = $line[0];
- $d = $line[$c + 1];
- @combin = @line[1..$c] if ($c ne "0");
- @oppose = @line[($c+2)..($c+$d+1)] if ($d ne "0");
- $string = $line[-1];
- @string = split('', $string);
- my @o = &invoke(\@combin, \@oppose, \@string);
- my $o = join(', ', @o);
- $o = "[".$o."]";
- printf "Case #%d: %s\n", $n, $o;
- $n++;
- }
- }
- close FIL;
- sub invoke{
- my ($c, $o, $s) = @_;
- my @c = @$c;
- my @o = @$o;
- my @s = @$s;
- my @output = ();
- foreach (@s){
- push(@output, $_);
- if (length(@output > 1) && length(@c > 0)){
- foreach (@c){
- my @pair = split('', substr $_, 0, 2);
- @pair = sort(@pair);
- my $t = substr $_, 2, 1;
- if (join('', sort(@output[-2, -1])) eq join('', @pair)){
- pop(@output);pop(@output);push(@output, $t);
- last;
- }
- }
- }
- if (length(@o > 0)){
- foreach (@o){
- my @opp = split('', $_);
- if (($opp[0] ~~ @output) && ($opp[-1] ~~ @output)){
- @output = ();
- last;
- }
- }
- }
- }
- return @output;
- }
复制代码 |
|