Syntactic Confectionery Delight PerlMonks

### Comment on

 Need Help??
The lcsbruteforce algorithm maintains this big table of solutions to subproblems. In this example, it's maintaining just the length of the LCS. Just change it to maintain the actual substring itself:
```sub lcsbruteforce {
my(\$x, \$y) = @_;
my(@v, \$cx, \$cy, \$left, \$above);
for my \$xi (0 .. length(\$x) - 1) {
\$cx = substr \$x, \$xi, 1;
for my \$yi (0 .. length(\$y) - 1) {
\$cy = substr \$y, \$yi, 1;
if (\$cx eq \$cy) {
#        \$v[\$xi][\$yi] = 1 + ((\$xi && \$yi) ? \$v[\$xi - 1][\$yi - 1] : 0);
\$v[\$xi][\$yi] = (\$xi && \$yi ? \$v[\$xi-1][\$yi-1] : "") . \$cx;
} else {
#        \$left = (\$xi && \$v[\$xi - 1][\$yi]) || 0;
#        \$above = (\$xi && \$v[\$xi][\$yi - 1]) || 0;
#        \$v[\$xi][\$yi] = (\$left > \$above) ? \$left : \$above;
\$left = (\$xi && \$v[\$xi - 1][\$yi]) || "";
\$above = (\$xi && \$v[\$xi][\$yi - 1]) || "";
\$v[\$xi][\$yi] = length(\$left) > length(\$above) ? \$left : \$above
+;

}
}
}
return \$v[length(\$x) - 1][length(\$y) - 1];
}
To change it to an actual brute force algorithm? That would be pretty strange. The brute force algorithm is:
```\$best = "";

for every subsequence \$s of \$x:
if \$s is also a subsequence of \$y:
\$best = \$s if length(\$s) > length(\$best);

return \$best;
Of course, the part where you get all subsequences and check for subsequence-ness is a pain. You can probably generate all subsequences using Algorithm::Loops, and perhaps use some regex stuff to check whether a string was a subsequence of another.

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?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2018-03-21 05:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
When I think of a mole I think of:

Results (263 votes). Check out past polls.

Notices?