in reply to Re: Can KMP be used to find the longest common subsequence?
in thread Can KMP be used to find the longest common subsequence?
The difference between a substring and a subsequence is that a subsequence is not necessarily contiguous (all the characters must be present, and in the correct order, but there can be unrelated characters in between).In that case, I think the easiest way to search for subsequences in Perl is to use regular expressions. For example, if the subsequence is "abcd", then the regular expression to look for a subsequence is /a.*?b.*?c.*?d/.
And since you can easily contruct a regex out of a string, that is easy toconstruct:
Result:$string = 'abcd'; $regex = join '.*?', split //, $string; $sequence = 'xxxxxaxxxxxxbxxxxxcxxxxdxxxxx'; if($sequence =~ /$regex/s) { printf "I found a match, at offset %d and with total length %d\n", + $-[0], $+[0]-$-[0]; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: Can KMP be used to find the longest common subsequence? (ugh)
by tye (Sage) on Dec 12, 2007 at 11:04 UTC | |
by bart (Canon) on Dec 12, 2007 at 11:46 UTC | |
by moritz (Cardinal) on Dec 12, 2007 at 12:08 UTC |