There's more than one way to do things PerlMonks

### Re^5: Longest Common Subsequence Question

by ikegami (Patriarch)
 on Nov 19, 2007 at 04:41 UTC ( #651584=note: print w/replies, xml ) Need Help??

in reply to Re^4: Longest Common Subsequence Question
in thread Longest Common Subsequence Question

oh, it doesn't have to be contiguous? Then yeah, your method would work.

Update: Pardon the disaster that was my participation in this thread.

• Comment on Re^5: Longest Common Subsequence Question

Replies are listed 'Best First'.
Re^6: Longest Common Subsequence Question (no)
by tye (Sage) on Nov 19, 2007 at 05:49 UTC

No, it wouldn't; it is just harder to come up with the counter example. The one that pops into my mind is due to the fact that an algorithm that claims to find "the longest common subsequence" will actually find "one of the maximal common subsequences" and you may have needed to find some other one:

```axbc
abxc
a1b2c

If the first run picks "axc" then you are doomed. If you get lucky and it picks "abc", then you win.

But I strongly suspect that you don't even need to rely on that quirk. Indeed, I now see that it is easy to extend the above example:

```axybc
abxyc
a1b2c

The solution is "abc" but you will first pick "axyc" and so won't include "b" in the final "answer".

- tye

Re^6: Longest Common Subsequence Question
by sgt (Deacon) on Nov 19, 2007 at 14:06 UTC

Humm Makes me think...:)

Actually mathematical subsequences are defined as maps on a subset of N and have "holes"; for example consider u(2n) versus u(n). Still in the case of finite sequences (especially in the context of char strings) it seems *contiguous* would be a fair (even natural) requisite.

Looking at some implementations seems to indicate that the contiguous case is considered as the common useful case:

```
% steph@ape (/home/stephan/w/tp) %
% perl -MString::LCSS -le 'print scalar String::LCSS::lcss(qw[xaxbcxdx
+ aybycyydy])'

% steph@ape (/home/stephan/w/tp) %
% perl -MString::LCSS -le 'print scalar String::LCSS::lcss(qw[xabcdxxx
+xxx yyyabcdyyyyy])'
abcd

I get the same with the recent String::LCSS_XS

```% steph@ape (/home/stephan/w/tp) %
% perl -MString::LCSS_XS -le 'print scalar String::LCSS_XS::lcss(qw[xa
+bcdxxxxxx yyyabcdyyyyy])'
abcd
cheers --stephan

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://651584]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2023-06-05 15:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
How often do you go to conferences?

Results (25 votes). Check out past polls.

Notices?