Relation between KMP algorithm and Z algorithm

后端 未结 3 2027
星月不相逢
星月不相逢 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:14

    Mikhail Melnik's solution may not compute Z for all indexes in a string like "aaaaa" we need an additional iteration to fill the indexes which are left empty in the first iteration.

    for i in range(0, len(s)):
        Z[i - lps[i] + 1] = lps[i]
    for i in range(0, len(s)):
        Z[i] = max(Z[i], Z[i - 1] - 1)                     `
    
    0 讨论(0)
  • 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".

    0 讨论(0)
  • 2021-02-03 14:31

    I think this will do it.

    def Z(lps):
        # First assume that we always need to restart when we find a mismatch.
        Z = [0] * len(lps)
    
        # Step through adjacent pairs.
        itr = enumerate(zip(lps, lps[1:]), start=1)
        for i, (prev, cur) in itr:
            if cur <= prev: # suffix stopped growing
                Z[i - prev] = prev # Mark this suffix at its beginning.
    
        # Ending the string is also a way to stop growing the suffix.
        if cur > 0: # if we were still growing a suffix
            # At end of loop, cur is the new prev, and i+1 is the new i.
            # (i == len(lps) - 1, cur == lps[-1])
            Z[i+1 - cur] = cur
    
        return Z
    

    Samples:

    Z([0,0,0,1,2]) #=> [0,0,0,2,0]
    Z([0,0,1,2,1,2]) #=> [0,0,2,0,2,0]
    
    0 讨论(0)
提交回复
热议问题