了解使用LCP阵列进行模式匹配的算法

时间:2015-02-11 00:04:45

标签: algorithm pattern-matching suffix-array

前言:我的问题主要是一个算法问题,所以即使你不熟悉后缀和LCP数组,你也可以帮助我。

this论文中,描述了如何有效地使用后缀和LCP数组进行字符串模式匹配。

我了解SA和LCP的工作原理以及如何从O(P*log(N))(其中P是模式的长度,N是字符串的长度)到{{}}来改进算法的运行时间{1}}(感谢Chris Eelmaa的回答here和jogojapans回答here)。

我试图通过图4中的算法来解释O(P+log(N))LLcp的用法。但我在理解它是如何工作方面遇到了问题。

算法(取自the source):

Pattern matching algorithm

使用的变量名称的说明:

RLcp

现在我想使用以下示例尝试该算法(部分取自here):

lcp(v,w) : Length of the longest common prefix of v and w
W = w0..wP-1 : pattern of length P
A = a0..aN-1 : the text (length N)
Pos[0..N-1] : suffix array
L_W : index (in A) of first occurrence of the matched pattern
M : middle index of current substring
L : lower bound
R : upper bound
Lcp : array of size N-2 such that Lcp[M] = lcp(A_Pos[L_M], A_pos[M]) where L_M is the lower bound of the unique interval with M in the middle
Rcp : array of size N-2 such that Rcp[M] = lcp(A_Pos[R_M], A_pos[M]) where R_M is the upper bound of the unique interval with M in the middle

我想尝试匹配一个字符串,比如 SA | LCP | Suffix entry ----------------------- 5 | N/A | a 3 | 1 | ana 1 | 3 | anana 0 | 0 | banana 4 | 0 | na 2 | 2 | nana A = "banana" ; N = 6 W = "ban" ; P = 3 ,并希望算法返回0为ban

以下是我将如何逐步完成算法:

L_W

我觉得我错过了什么,但我找不到什么。另外,我想知道如何使用预先计算的LCP数组而不是调用l = lcp("a", "ban") = 0 r = lcp("nana", "ban") = 0 if 0 = 3 or 'b' =< 'a' then // which is NOT the case for both conditions L_W = 0 else if 0 < 3 or 'b' =< 'n' then // which is the case for both conditions L_W = 6 // which means 'not found' ... ...

1 个答案:

答案 0 :(得分:1)

我相信有一个错误。

第一个条件相当容易理解。当LCP长度==模式长度时,就完成了。当您的模式甚至小于或等于最小模式时,只有选择是最小的模式。

第二个条件是错误的。我们可以通过矛盾来证明这一点。 r&lt; P || Wr&lt; = a ...表示r> = P&amp;&amp; Wr&gt; a ...如果r> = P,那么我们怎么能得到Lw = N(未找到),因为我们已经有r长度的公共前缀?

相关问题