### comment on

 Need Help??

One method that might work well enough would be to concatenate the strings and then look for the longest non-overlapping repetition. You'll have to worry about string boundaries though. Here's some code for finding the longest repeated substring in case you take to the idea:

```sub repeated_substring {
my (\$ssl,\$pos) = (1,-1);
my \$len = length(\$_[0]);
my \$i = 0;
while (\$i < \$len-2*\$ssl) {
if (index(\$_[0],substr(\$_[0],\$i,\$ssl),\$i+\$ssl) == -1) { \$i++; ne
+xt }
\$pos = \$i; \$ssl++;
}
return \$pos == -1 ? "" : substr(\$_[0], \$pos, \$ssl-1);
}

If you joined the strings with some character that won't appear in the strings (a colon say), then you could modify the above such that as soon as you hit a colon, stop

Update: I just noticed that you're blindly grabbing the first element in the list. An optimization would be to sort the list of strings by length and always start with the shortest one (assuming you continue using your method).

DOH! I just realized that my method won't work at all!

Update: Okay ... I'm stubborn. I know it. Here's how to *make* it work with the repeated_substring() routine:

```sub findlcs {
my @ret;
for my \$i (0..\$#_-1) {
for my \$j (\$i..\$#_) {
my \$str = join ":", @_[\$i,\$j];
my \$ans = repeated_substring(\$str);
push @ret, \$ans;
}
}
return (sort { length(\$a) <=> length(\$b) } @ret)[0];
}
Whew! Boy is that inefficient and ugly! :-)

revdiablo, you did it well. Don't knock your implementation.

Another! update: I realized on my way to pick up the kids that even this method fails. I sure hope revdiablo is watching this to see what could have happened to him :-)

In reply to Re: finding longest common substring by duff
in thread finding longest common substring by revdiablo

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.
• Please read these before you post! —
• 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?
• See Writeup Formatting Tips and other pages linked from there for more info.

Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2020-11-30 05:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?

No recent polls found

Notices?