Result: 8
The minimum length B is “ababbaba”
A can be obtained from B this way:
ababbababbabababbabababbababbaba
ababbaba
ababbaba
ababbaba
ababbaba
ababbaba
Solution:
The simplest solution uses suffix arrays a balanced tree and a max-heap. It’s obvious that the
searched pattern is a prefix of A. Having
sorted the suffixes of A, we shall add to B,
step by step, one
more ending character. At each step we keep to pointers L and R, representing the
first and the last suffix
in the array that have B as a prefix. The balanced tree will hold permanently the starting positions of the
suffixes between L and R, and the heap will keep the distances between consecutive elements of the tree.
When inserting
a new character into B, by two binary searches we get the new L’ and R’, with L
≤
L’ and R’
≤
R. We must also update the tree and the heap. Introduce
characters into B while the
biggest(first) element in the heap is smaller or equal to the length of B. B’s final length offers the searched
result. The final complexity is O(n lg n), where n is the length of A.
Dostları ilə paylaş: