|
The longest common substring problem is to find the longest string (or strings) that is a substring (or are substrings) of two or more strings. It should not be confused with the longest common subsequence problem. (For an explanation of the difference between a substring and a subsequence, see substring).
ExampleThe longest common substrings of the strings "ABAB", "BABA" and "ABBA" are the strings "AB" and "BA" of length 2. Other common substrings are "A", "B" and the empty string "". ABAB ||| BABA || ABBA Problem definitionGiven two strings, S of length m and T of length n, find the longest strings which are a substrings of both S and T. A generalisation is the k-common substring problem. Given the set of strings S = {S1,...,SK}, where | Si | = ni and Σni = N. Find for each 2 ≤ k ≤ K, the longest strings which occur as substrings of at least k strings. AlgorithmsYou can find the lengths and starting positions of the longest common substrings of S and T in Θ(n + m) with the help of a generalised suffix tree. Finding them by dynamic programming costs Θ(nm). The solutions to the generalised problem take Θ(n1 + ... + nK) and Θ(n1·...·nK) time. Suffix treeYou can find the longest common substrings of a set of strings by building a generalised suffix tree for the strings, and then finding the deepest internal nodes which has leaf nodes from all the strings in the subtree below it. In the figure on the right you see the suffix tree for the strings "ABAB", "BABA" and "ABBA", padded with unique string terminators, to become "ABAB$0", "BABA$1" and "ABBA$2". The nodes representing "A", "B", "AB" and "BA" all have descendant leaves from all of the strings, numbered 0, 1 and 2. Building the suffix tree takes Θ(n) time (if the size of the alphabet is constant). If you traverse the tree bottom up, and maintain a bit vector telling which strings are seen below each node, you can solve the k-common substring problem in Θ(NK) time. If you prepare your suffix tree for constant time lowest common ancestor retrieval, you can solve it in Θ(N) time.[1] Dynamic programmingYou first find the longest common suffix for all pairs of prefixes of the strings. The longest common suffix is
For the example strings "ABAB" and "BABA":
This can be extended to more than two strings by adding more dimensions to the table. PseudocodeThe following pseudocode finds the set of longest common substrings between two strings with dynamic programming:
function LCSubstr(S[1..m], T[1..n])
L := array(0..m, 0..n)
z := 0
ret := {}
for i := 1..m
for j := 1..n
if S[i] = T[j]
if i = 1 or j = 1
L[i,j] := 1
else
L[i,j] := L[i-1,j-1] + 1
if L[i,j] > z
z := L[i,j]
ret := {}
if L[i,j] = z
ret := ret ∪ {S[i-z+1..i]}
return ret
This algorithm runs in O(mn) time. The variable The following tricks can be used to reduce the memory usage of an implementation:
References
External links |
This article is from Wikipedia. All text is available under the terms of the GNU Free Documentation License.
Mercedes Car
This site monitored by SitePinger.net