首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >理解LCP阵列的模式匹配算法

理解LCP阵列的模式匹配算法
EN

Stack Overflow用户
提问于 2015-02-11 00:04:45
回答 1查看 980关注 0票数 2

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

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

我理解SA和LCP的工作原理,以及如何将算法的运行时从O(P*log(N)) ( P是模式的长度,N是字符串的长度)改进到O(P+log(N)) (感谢Chris的答案这里和jogojapans的应答这里)。

我试图通过图4中的算法来解释LLcpRLcp的用法。但我很难理解它是如何工作的。

该算法(取自来源):

对使用的变量名的解释:

代码语言:javascript
复制
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

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

代码语言:javascript
复制
 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。

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

代码语言:javascript
复制
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)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-15 13:04:32

我相信这是个错误。

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

第二个条件是错误的。我们可以用矛盾来证明这一点。<= a.意思是r >= P& Wr >a.如果r >= P,那么我们怎么可能有Lw =N(未找到),因为我们已经有r长度的公共前缀了?

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28444226

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档