Files
rose-ash/lib/ocaml/baseline/manacher.ml
giles 4fdf6980da
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
ocaml: parser accepts if/match/let/fun as rhs of <- and :=
Previously `a.(i) <- if c then x else y` failed with
"unexpected token keyword if" because parse-binop-rhs called
parse-prefix for the rhs, which doesn't accept if/match/let/fun.

Real OCaml allows full expressions on the rhs of <-/:=. Fix:
special-case prec-1 ops in parse-binop-rhs to call parse-expr-no-seq
instead of parse-prefix. The recursive parse-binop-rhs with
min-prec restored after picks up any further chained <- (since both
ops are right-associative with no higher-prec binops above them).

Manacher baseline updated to use bare `if` on rhs of <-,
removing the parens workaround from iter 235. 607/607 regressions
remain clean.
2026-05-10 06:11:57 +00:00

33 lines
759 B
OCaml

let manacher s =
let n = String.length s in
let m = 2 * n + 1 in
let t = Array.make m '#' in
for i = 0 to n - 1 do
t.(2 * i + 1) <- s.[i]
done;
let p = Array.make m 0 in
let center = ref 0 and right = ref 0 in
let max_p = ref 0 in
for i = 0 to m - 1 do
let mirror = 2 * !center - i in
if i < !right then begin
let v = !right - i in
let pm = p.(mirror) in
p.(i) <- if pm < v then pm else v
end;
while i + p.(i) + 1 < m && i - p.(i) - 1 >= 0
&& t.(i + p.(i) + 1) = t.(i - p.(i) - 1) do
p.(i) <- p.(i) + 1
done;
if i + p.(i) > !right then begin
center := i;
right := i + p.(i)
end;
if p.(i) > !max_p then max_p := p.(i)
done;
!max_p
;;
manacher "babadaba"