This code was written as a solution to the problem posed in Search for identical substrings. As best I can tell it runs about 3 million times faster than the original code.
The code reads a series of strings and searches them for the longest substring between any pair of strings. In the original problem there were 300 strings about 3K long each. A test set comprising 6 strings was used to test the code with the result given below.
Someone with Perl module creation and publication experience could wrap this up and publish it if they wish.
use List::Util qw(min max);
my $allLCS = 1;
my $subStrSize = 8; # Determines minimum match length. Should be a pow
+er of 2
# and less than half the minimum interesting match length. The larger
# the faster the search runs.
if (@ARGV != 1)
print "Finds longest matching substring between any pair of test s
print "the given file. Pairs of lines are expected with the first
print "pair being the string name and the second the test string."
# Read in the strings
my $strName = $_;
$_ = <>;
push @strings, [$strName, $_];
my $lastStr = @strings - 1;
my @bestMatches = [(0, 0, 0, 0, 0)]; # Best match details
my $longest = 0; # Best match length so far (unexpanded)
my $startTime = [Time::HiRes::gettimeofday ()];
# Do the search
my $curStr = $_;
my $source = $strings[$curStr];
my $sourceName = $strings[$curStr];
for (my $i = 0; $i < length $source; $i += $subStrSize)
push @subStrs, substr $source, $i, $subStrSize;
my $lastSub = @subStrs-1;
my $targetStr = $_;
my $target = $strings[$_];
my $targetLen = length $target;
my $targetName = $strings[$_];
my $localLongest = 0;
my @localBests = [(0, 0, 0, 0, 0)];
for my $i (0..$lastSub)
my $offset = 0;
while ($offset < $targetLen)
$offset = index $target, $subStrs[$i], $offset;
last if $offset < 0;
my $matchStr1 = substr $source, $i * $subStrSize;
my $matchStr2 = substr $target, $offset;
($matchStr1 ^ $matchStr2) =~ /^\0*/;
my $matchLen = $+;
next if $matchLen < $localLongest - $subStrSize + 1;
$localLongest = $matchLen;
my @test = ($curStr, $targetStr, $i * $subStrSize, $offset, $m
@test = expandMatch (@test);
my $dm = $test - $localBests[-1];
@localBests = () if $dm > 0;
push @localBests, [@test] if $dm >= 0;
$offset = $test + $test;
next if $test < $longest;
$longest = $test;
$dm = $longest - $bestMatches[-1];
next if $dm < 0;
@bestMatches = () if $dm > 0;
push @bestMatches, [@test];
next if ! $allLCS;
if (! @localBests)
print "Didn't find LCS for $sourceName and $targetName\n";
my @curr = @$_;
printf "%03d:%03d L[%4d] (%4d %4d)\n",
$curr, $curr, $curr, $curr, $curr;
print "Completed in " . Time::HiRes::tv_interval ($startTime) . "\n";
my @curr = @$_;
printf "Best match: %s - %s. %d characters starting at %d and %d.\n"
$strings[$curr], $strings[$curr], $curr, $curr,
my ($index1, $index2, $str1Start, $str2Start, $matchLen) = @_;
my $maxMatch = max (0, min ($str1Start, $subStrSize + 10, $str2Start))
my $matchStr1 = substr ($strings[$index1], $str1Start - $maxMatch,
my $matchStr2 = substr ($strings[$index2], $str2Start - $maxMatch,
($matchStr1 ^ $matchStr2) =~ /\0*$/;
my $adj = $+ - $-;
$matchLen += $adj;
$str1Start -= $adj;
$str2Start -= $adj;
return ($index1, $index2, $str1Start, $str2Start, $matchLen);
Output using bioMan's six string sample:
Updates: fixed a few bugs. Added print all LCS's option.
Completed in 0.010486
Best match: >string 1 - >string 3 . 1271 characters starting at 82 an
Fixed readmore tags.
Fixed all remaining known bugs. Added reporting for LCS's between each pair and for all LCS's where there is more than one LCS of the maximum length. Code now 20% smaller and maybe 10% slower.
Perl is Huffman encoded by design.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
| & || & |
| < || < |
| > || > |
| [ || [ |
| ] || ] ||