Documentation

CslibFrontier.Foundations.Data.List.CommonPrefix

Length of the longest common prefix of two lists #

List.lcpLen u v is the length of the longest common prefix of u and v. The string algorithms use it as a specification: suffix and LCP arrays (CslibFrontier.Algorithms.Lean.SuffixArray) and the Z-function (CslibFrontier.Algorithms.Lean.ZFunction).

Main definitions #

Main results #

def List.lcpLen {α : Type u_1} [DecidableEq α] :
List α → List α → ℕ

Length of the longest common prefix of u and v.

@[simp]
theorem List.lcpLen_nil_left {α : Type u_1} [DecidableEq α] (v : List α) :
[].lcpLen v = 0
@[simp]
theorem List.lcpLen_nil_right {α : Type u_1} [DecidableEq α] (u : List α) :
u.lcpLen [] = 0
theorem List.lcpLen_cons_cons {α : Type u_1} [DecidableEq α] (a b : α) (u v : List α) :
(a :: u).lcpLen (b :: v) = if a = b then u.lcpLen v + 1 else 0
@[simp]
theorem List.lcpLen_cons_cons_self {α : Type u_1} [DecidableEq α] (a : α) (u v : List α) :
(a :: u).lcpLen (a :: v) = u.lcpLen v + 1
@[simp]
theorem List.lcpLen_self {α : Type u_1} [DecidableEq α] (u : List α) :
theorem List.lcpLen_comm {α : Type u_1} [DecidableEq α] (u v : List α) :
u.lcpLen v = v.lcpLen u
theorem List.lcpLen_le_length_left {α : Type u_1} [DecidableEq α] (u v : List α) :
theorem List.lcpLen_le_length_right {α : Type u_1} [DecidableEq α] (u v : List α) :
theorem List.le_lcpLen_iff {α : Type u_1} [DecidableEq α] {u v : List α} {k : ℕ} :
k ≤ u.lcpLen v ↔ k ≤ u.length ∧ k ≤ v.length ∧ take k u = take k v

k ≤ lcpLen u v iff both lists have length at least k and agree on their first k entries.

theorem List.lcpLen_eq_add_lcpLen_drop {α : Type u_1} [DecidableEq α] {u v : List α} {h : ℕ} (hh : h ≤ u.lcpLen v) :
u.lcpLen v = h + (drop h u).lcpLen (drop h v)

A common prefix of length h splits off the longest common prefix.

theorem List.lcpLen_sub_one_le_lcpLen_tail {α : Type u_1} [DecidableEq α] (u v : List α) :

Dropping one leading entry from both lists shortens their longest common prefix by at most one.

theorem List.min_lcpLen_eq_of_take_eq {α : Type u_1} [DecidableEq α] {u v : List α} (x : List α) {m : ℕ} (h : take m u = take m v) :
min (x.lcpLen u) m = min (x.lcpLen v) m

Lists that agree on their first m entries have the same longest common prefix with any list x, up to m.

theorem List.length_le_lcpLen_append_iff {α : Type u_1} [DecidableEq α] {u x v : List α} :
u.length ≤ (u ++ x).lcpLen v ↔ u <+: v

u ++ x and v share their first |u| entries iff u is a prefix of v.

theorem List.lcpLen_le_lcpLen_of_le_of_le_left {α : Type u_1} [LinearOrder α] {u v x : List α} (huv : u ≤ v) (hvx : v ≤ x) :
u.lcpLen x ≤ u.lcpLen v

A list lexicographically between u and x shares at least lcpLen u x leading entries with u.

theorem List.lcpLen_le_lcpLen_of_le_of_le_right {α : Type u_1} [LinearOrder α] {u v x : List α} (huv : u ≤ v) (hvx : v ≤ x) :
u.lcpLen x ≤ v.lcpLen x

A list lexicographically between u and x shares at least lcpLen u x leading entries with x.