#################################### sub LCCOS { #################################### my ($array1,$array2) = @_; my $current_largest = []; my $current_size; if ( @$array1 > @$array2 ) { ($array1,$array2) = ($array2,$array1) }; my $count = 0; my %elements_in_longer; #create an HoA holding the locations of all elements #in the larger array so we only start at those places push @{$elements_in_longer{$_}}, $count++ for @$array1; for my $pos_in_small ( 0 .. $#array2 ) { my $current_letter = $array2->[$pos_in_small]; for my $possible_start(@{$elements_in_longer{$current_letter}}) { my $curr_pos_in_small = $pos_in_small; my $curr_pos_in_large = $possible_start; my @curr_matching_sequence; while ( defined $array1->[$curr_pos_in_large] and defined $array2->[$curr_pos_in_small] ) { if ($array1->[$curr_pos_in_large] eq $array2->[$curr_pos_in_small]) { push @curr_matching_sequence, $array2->[$curr_pos_in_small]; $curr_pos_in_large++; $curr_pos_in_small++; } else { last; } } if ( @curr_matching_sequence > @{$current_largest} ) { $current_largest = \@curr_matching_sequence; $current_size = @curr_matching_sequence; } } #if the $current_size is longer that what is left to #check in the smaller array, then we are done. last if $current_size > $#array2 - $pos_in_small; } return $current_largest; } ##LCCOS