Relation between KMP algorithm and Z algorithm

后端 未结 3 2026
星月不相逢
星月不相逢 2021-02-03 13:43

KMP and Z algorithms are well known algorithms for string searching,

KMP algorithm deals with finding the patterns through a KMP f

3条回答
  •  生来不讨喜
    2021-02-03 14:31

    NOTE: algorithm is wrong

    for i in range(0, len(s)):
        if lps[i] != 0:
            Z[i - lps[i] + 1] = lps[i]
    

    After that in Z[i] will be the maximum length of the suffix, that starts in position i and which is also a prefix of the string.

    EDIT

    As nikhil_vyas noted, the proposed algorithm does not solve your problem. What it actually does is partially filling Z array with the longest suffixes and some others. Such incomplete array can basically help you to solve several "find the longest something in the string" problems, but it does not answer your question.

    The easiest way to rebuild Z array having lps array that comes to my mind is to build the string, corresponding to the lps array and then build Z array for that string. But I am not sure whether it suits your definition of "some modifications in the lps array".

提交回复
热议问题