前言:我的问题主要是算法问题,所以即使你不熟悉后缀和LCP数组,你也可以帮我。
在这文件中,描述了如何有效地使用后缀和LCP数组进行字符串模式匹配。
我理解SA和LCP的工作原理,以及如何将算法的运行时从O(P*log(N)) ( P是模式的长度,N是字符串的长度)改进到O(P+log(N)) (感谢Chris的答案这里和jogojapans的应答这里)。
我试图通过图4中的算法来解释LLcp和RLcp的用法。但我很难理解它是如何工作的。
该算法(取自来源):

对使用的变量名的解释:
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我想尝试匹配一个字符串,比如ban,并期望算法以L_W的形式返回0。
下面是我将如何逐步完成该算法:
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'
...
...我觉得我错过了什么,但我不知道是什么。此外,我还想知道如何使用预计算的LCP数组而不是调用lcp(v,w)。
发布于 2018-04-15 13:04:32
我相信这是个错误。
第一个条件很容易理解。当LCP长度为==模式长度时,就完成了。当您的模式甚至小于或等于最小的模式,那么只有选择是最小的。
第二个条件是错误的。我们可以用矛盾来证明这一点。<= a.意思是r >= P& Wr >a.如果r >= P,那么我们怎么可能有Lw =N(未找到),因为我们已经有r长度的公共前缀了?
https://stackoverflow.com/questions/28444226
复制相似问题