is there any paper or an explanation on how to implement a two dimensional KMP?

江枫思渺然 提交于 2020-01-01 05:54:26

问题


I tried to solve the problem of two dimensional search using a combination of Aho-Corasick and a single dimensional KMP, however, I still need something faster.

To elaborate, I have a matrix A of characters of size n1*n2 and I wish to find all occurrences of a smaller matrix B of size m1*m2 and I want that to be in O(n1*n2+m1*m2) if possible.

For example:

A = a b c b c b
    b c a c a c
    d a b a b a
    q a s d q a

and

B = b c b
    c a c
    a b a

the algorithm should return the indexes of say, the upper left corner of the match, which in this case should return (0,1) and (0,3). notice that the occurrences may overlap.


回答1:


There is an algorithm called the Baker-Bird algorithm that I just recently encountered that appears to be a partial generalization of KMP to two dimensions. It uses two algorithms as subroutines - the Aho-Corasick algorithm (which itself is a generalization of KMP), and the KMP algorithm - to efficiently search a two-dimensional grid for a pattern.

I'm not sure if this is what you're looking for, but hopefully it helps!



来源:https://stackoverflow.com/questions/9302735/is-there-any-paper-or-an-explanation-on-how-to-implement-a-two-dimensional-kmp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!