laziness, impatience, and hubris PerlMonks

### Comment on

 Need Help??

If you understood the mental process I went through to create the Longest Common Subsequence, it should not be hard to see how to adapt it to the Longest Common Substring.

Once we have a list of character positions across strings, we can create ordered piles where each mapping has a difference of 1 in each position with its adjacent neighbors. You can get rid of the need for looping to find mappings greater because you can predict the values they should contain. Then you just need to find the largest pile and that represents the LCS (S = substring).

Arrays are replaced with hashes. Picking a hash key at random, you place it in a new pile and then predict the next mapping. If that key exists you place it on the same pile. You repeat this process until you have no more mappings. You then start back at the beginning of the pile and look for smaller items. Once no more items fit in that pile, you move on to the next hash key.

```use Algorithm::Loops 'NestedLoops';
use List::Util 'reduce';

my @str = map {chomp; \$_} <DATA>;
print LCS(@str), "\n";

sub LCS{
my @str = @_;
my @pos;
for my \$i (0 .. \$#str) {
my \$line = \$str[\$i];
for (0 .. length(\$line) - 1) {
my \$char= substr(\$line, \$_, 1);
push @{\$pos[\$i]{\$char}}, \$_;
}
}
my \$sh_str = reduce {length(\$a) < length(\$b) ? \$a : \$b} @str;
my %map;
CHAR:
for my \$char (split //, \$sh_str) {
my @loop;
for (0 .. \$#pos) {
next CHAR if ! \$pos[\$_]{\$char};
push @loop, \$pos[\$_]{\$char};
}
my \$next = NestedLoops([@loop]);
while (my @char_map = \$next->()) {
my \$key = join '-', @char_map;
\$map{\$key} = \$char;
}
}
my @pile;
for my \$seq (keys %map) {
push @pile, \$map{\$seq};
for (1 .. 2) {
my \$dir = \$_ % 2 ? 1 : -1;
my @offset = split /-/, \$seq;
\$_ += \$dir for @offset;
my \$next = join '-', @offset;
while (exists \$map{\$next}) {
\$pile[-1] = \$dir > 0 ? \$pile[-1] . \$map{\$next} : \$map{
+\$next} . \$pile[-1];
\$_ += \$dir for @offset;
\$next = join '-', @offset;
}
}
}
return reduce {length(\$a) > length(\$b) ? \$a : \$b} @pile;
}
__DATA__
qwertyJoyzhnac
Jyzshuaqwertyb
Joyzqwertybbc
Yqwertyblah

Cheers - L~R

In reply to Re^2: Longest Common Subsequence by Limbic~Region
in thread Longest Common Subsequence by Limbic~Region

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

• 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.
• Posts may use any of the Perl Monks Approved HTML tags:
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
• You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
 For: Use: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?

Create A New User
Chatterbox?
 [karlgoethebier]: btw, a good day... marto waves

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2017-12-12 11:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
What programming language do you hate the most?

Results (330 votes). Check out past polls.

Notices?