#! perl -slw use strict; use Time::HiRes qw[ time ]; use Data::Dump qw[ pp ]; ## Generate test data my @paths = map[ map chr(65+rand 26), 2 .. 2+rand(11) ], 1 .. 32448; my $start = time; ## Sort by length of subarray; shortest last to facilitate deletion in a loop @paths = sort{ $#{ $b } <=> $#{ $a } } @paths; #pp \@paths; ## Build an index to the elements in all the subarrays my( $i, %index ) = 0; for my $ref ( @paths ){ $index{ $_ } //= ++$i for @$ref; } #pp \%index; ## Use the index to build bitmaps representing the subarrays my @bitmaps = ( '' ) x @paths; for my $n ( 0 .. $#paths ) { vec( $bitmaps[ $n ], $index{ $_ }, 1 ) = 1 for @{ $paths[ $n ] }; } #pp \@bitmaps; ## bitwise OR the bitmaps to discover wholly contained subarrays and remove them OUTER:for my $i ( reverse 0 .. $#paths ) { for my $j ( 0 .. $i-1 ) { ## Remove reverse; short-circuit earlier. if( ( $bitmaps[ $j ] | $bitmaps[ $i ] ) eq $bitmaps[ $j ] ) { # warn "deleting $i"; delete $paths[ $i ]; next OUTER; } } } ## remove undefs @paths = grep defined, @paths; printf "Deleted %u subarrays\n", 32448 - @paths; printf "Took %.6f seconds\nEnter to see results:", time() - $start; ; pp \@paths; __END__ C:\test>1169736.pl Deleted 22228 subarrays Took 107.267549 seconds Enter to see results:Terminating on signal SIGINT(2) C:\test>1169736.pl Deleted 22024 subarrays Took 108.820184 seconds Enter to see results:Terminating on signal SIGINT(2)