-
-
Save ology/7f9972167a739b3a64a04c623c9aa9f3 to your computer and use it in GitHub Desktop.
ordered subsequence with possible duplicates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sub contiguous_subsequences { | |
my ($little_set, $big_set) = @_; | |
my $little_set_size = @$little_set; | |
my $big_set_size = @$big_set; | |
my @matches; | |
my ($i, $j) = (0, 0); # sequence indices | |
my $k = 0; # matches found | |
my $p = 0; # initial big set position | |
my $f = 0; # failed to find match | |
while ($i < $little_set_size && $j < $big_set_size) { | |
if ($little_set->[$i] eq $big_set->[$j]) { | |
$k++; # We match | |
$j++; # Move to the next element in the big_set | |
$f = 0; # reset failure flag | |
} | |
else { | |
$f++; # we failed to match | |
} | |
$i++; # Move to the next element in the little_set | |
if ($f || $i >= $little_set_size) { | |
push @matches, $p if $k == $little_set_size; | |
$p++; # increment the big set position | |
$i = 0; # reset the little set position | |
$j = $p; # set the big set index to the big set position | |
$k = 0; # no matches seen | |
} | |
} | |
return \@matches; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment