• + 0 comments

    Perl:

    sub acmTeam {
        my $topics = shift;
        my $n = @$topics;
        my $m = $n > 0? length($topics->[0]): 0;
        my $max = 0;
        my $max_n = 0;
        for my $i (0 .. $n-2) {
            for my $j ($i + 1 .. $n-1) {
                my $st = 0;
                $st += (substr($topics->[$i], $_, 1) eq '1' || substr($topics->[$j], $_, 1) eq '1'? 1: 0) for (0..$m-1);
                if($st > $max) {
                    $max = $st;
                    $max_n = 1;
                } elsif($st == $max) {
                    ++$max_n;
                }
            }
        }
        return ($max, $max_n);
    }