GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing en.wikipedia.org — Longest common substring - Wikipedia (Redirected from Longest common substring problem) Wikibooks has a book on the topic of: Algorithm Implementation/Strings/Longest common substring Not to be confused with longest common subsequence.
In computer science, a longest common substring of two or more strings is a longest string that is a substring of all of them. There may be more than one longest common substring. Applications include data deduplication and plagiarism detection. Unlike the longest common subsequence problem, which finds insertions or deletions within the common text, the longest common substring problem seeks a contiguous substring shared by both texts. ExamplesThe picture shows two strings where the problem has multiple solutions. Although the substring occurrences always overlap, it is impossible to obtain a longer common substring by "uniting" them. The strings "ABABC", "BABCA" and "ABCBA" have only one longest common substring, viz. "ABC" of length 3. Other common substrings are "A", "AB", "B", "BA", "BC" and "C". ABABC
|||
BABCA
|||
ABCBA
Problem definitionGiven two strings, of length and of length , find a longest string which is substring of both and . A generalization is the k-common substring problem. Given the set of strings , where and . Find for each , a longest string which occurs as substring of at least strings. AlgorithmsOne can find the lengths and starting positions of the longest common substrings of and in time with the help of a generalized suffix tree. A faster algorithm can be achieved in the word RAM model of computation if the size of the input alphabet is in . In particular, this algorithm runs in time using space.[1] Solving the problem by dynamic programming costs . The solutions to the generalized problem take space and time with dynamic programming and take time with a generalized suffix tree. Suffix treeThe longest common substrings of a set of strings can be found by building a generalized suffix tree for the strings, and then finding the deepest internal nodes which have leaf nodes from all the strings in the subtree below it. The figure on the right is 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 time (if the size of the alphabet is constant). If the tree is traversed from the bottom up with a bit vector telling which strings are seen below each node, the k-common substring problem can be solved in time. If the suffix tree is prepared for constant time lowest common ancestor retrieval, it can be solved in time.[2] Dynamic programmingThe following pseudocode finds the set of longest common substrings between two strings with dynamic programming: function LongestCommonSubstring(S[1..r], T[1..n])
L := array(1..r, 1..n)
z := 0 # length of the longest common substring found so far
ret := {}
for i := 1..r
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 := {S[(i − z + 1)..i]}
else if L[i, j] = z
ret := ret ∪ {S[(i − z + 1)..i]}
else
L[i, j] := 0
return ret
This algorithm runs in time. The array The following tricks can be used to reduce the memory usage of an implementation:
See also
References
External linksThe Wikibook Algorithm implementation has a page on the topic of: Longest common substring
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |