Relation between KMP algorithm and Z algorithm

后端 未结 3 2029
星月不相逢
星月不相逢 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)                     `
    

提交回复
热议问题