Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Longest common contiguous subsequence - algorithm

My question is simple: Is there an O(n) algorithm for finding the longest contiguous subsequence between two sequences A and B? I searched it, but all the results were about the LCS problem, which is not what I'm seeking.

Note: if you are willing to give any sample code, you are more than welcome to do so, but please, if you can, in C or C++.

Edit: Here is an example:

A: { a, b, a, b, b, b, a }
B: { a, d, b, b, b, c, n }
longest common contiguous subsequence: { b, b, b }
like image 946
Rontogiannis Aristofanis Avatar asked Dec 25 '12 18:12

Rontogiannis Aristofanis


1 Answers

Yes, you can do this in linear time. One way is by building suffix trees for both the pattern and the text and computing their intersection. I can't think of a way to do this without involving suffix trees or suffix arrays, though.

like image 177
tmyklebu Avatar answered Sep 27 '22 23:09

tmyklebu