fork download
  1. #!/usr/bin/perl -w
  2.  
  3. use strict;
  4.  
  5. my @AoA = (
  6. ['a','b','c'],
  7. ['a','b','c'],
  8. ['a','b','d'],
  9. ['a','b','d'],
  10. [ qw(a b c) ],
  11. [ qw(c b a) ],
  12. [ qw(b c a) ],
  13. [ qw(a b d) ],
  14. );
  15.  
  16. my %combinations;
  17. feedme(@AoA);
  18.  
  19.  
  20. sub feedme {
  21. my %uniques;
  22.  
  23. for my $ary ( @_ ) {
  24. my $sample = join '', @$ary;
  25.  
  26. if ( exists $combinations{ $sample } ) {
  27. ++$uniques{ $combinations{ $sample } };
  28. } else {
  29. fill_combinations( @$ary );
  30. ++$uniques{ $combinations{ $sample } };
  31. }
  32. }
  33.  
  34. for (keys %uniques) {
  35. printf "%s found %d times\n", $_, $uniques{ $_ };
  36. }
  37. }
  38.  
  39. sub fill_combinations(@) {
  40. # Note: this function needs to be reworked for >3 elements in an array
  41. my $combination = join '', @_;
  42.  
  43. my $index = 0;
  44. while ( $index < @_ ) {
  45. my @left = split $_[$index], $combination;
  46. my $fw = join '', $_[$index], @left;
  47. my $rv = join '', $_[$index], scalar reverse @left;
  48. $combinations{ $fw } = $combination;
  49. $combinations{ $rv } = $combination;
  50. $index++;
  51. }
  52. }
  53.  
Success #stdin #stdout 0s 6000KB
stdin
Standard input is empty
stdout
abd found 3 times
abc found 5 times