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]; }
In reply to Re^2: Can KMP be used to find the longest common subsequence?
by bart
in thread Can KMP be used to find the longest common subsequence?
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |