Documentation

CslibFrontier.Algorithms.Lean.ZFunction.ToPrefix

Converting the Z table to the KMP prefix table #

A proper border of length k in the prefix ending at position t - 1 starts at j = t - k. The Z value at j covers that border precisely when k ≤ z[j]. Thus the earliest positive starting position gives the longest border. Once the Z value at a position fails to reach the current end, it also fails at every later end, and the scan can discard that position.

zToPrefix reads only the table. The convention z[0] = n is immaterial because a proper border starts after zero. zToPrefix_zArray identifies the result with the KMP construction for every word, including the empty word.

@[irreducible]

Scan the Z table for the earliest positive start covering the prefix of length t. The entries in acc concern the shorter prefixes. A failed comparison advances the candidate start j; a successful one emits the border length and advances the prefix end. At j = t the length is zero regardless of the table entry, including at the right boundary.

Compute the KMP prefix table from the Z table, with entry t - 1 giving the longest proper border of the prefix of length t when the input is a realizable Z table.

@[simp]

Every input entry has one corresponding output entry, including for unrealizable tables.

A proper border of a prefix is exactly a match of the same length at its starting position.

Converting the Z table of any array gives its KMP prefix table.