ただしこれは(123)(456) と (456)(123)を両方数えている。
combin(8, 3) * combin(5, 3) = 8*7*5*4 = 560通り。
use strict;
use warnings;
my $haipai = shift;
if(!$haipai){print "specify haipai.\n"; exit;};
print "haipai : $haipai\n";
my $max_menz = int(length($haipai) / 3);
print "max_menz : $max_menz\n";
my @haipai = split("", $haipai);
my $level=0;
my $count = 0;
my @found;
my @result;
&select_menz(\@haipai);
$count = 0;
for(@result){
$count++;
print "$count:$_\n";
}
sub select_menz{
my ($array_ref) = @_;
my ($i, $j, $k) = (0, 1, 2);
# print "array:@$array_ref\n";
while($i<$#$array_ref-1){
$count++;
# print "level: $level num of found: $#found - $i $j $k - $count : ".$array_ref->[$i].$array_ref->[$j].$array_ref->[$k]."\n";
# print "found:@found\n";
if($max_menz == 3){
if($#$array_ref > 4 && $#found == 2) {
pop @found;
pop @found;
pop @found;
}
}
if($level == 0 && $#found == $max_menz - 1 ) {
pop @found;
pop @found;
}elsif($level == 1 && $#found == $max_menz - 1 ){
pop @found;
}
push @found, "(".$array_ref->[$i].$array_ref->[$j].$array_ref->[$k].")";
if($#found > $max_menz - 2 ){
push @result, join("", sort @found);
}
my @new_array = @$array_ref;
$new_array[$i]=0;
$new_array[$j]=0;
$new_array[$k]=0;
@new_array = grep{ $_ > 0 } @new_array;
if($#new_array > 1){
$level++;
&select_menz(\@new_array);
}
$k++;
if($k>$#$array_ref){
$j++;
$k=$j+1;
if($j>$#$array_ref-1){
$i++;
$j=$i+1;
$k=$j+1;
}
}
}
$level = 0;
# print "--- end of loop --- $#$array_ref \n";
}