ocaml: phase 5.1 manacher.ml baseline (longest palindrome "babadaba" = 7)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 25s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 25s
Manacher's algorithm: insert # separators (length 2n+1) to unify odd/even cases, then maintain palindrome radii p[] alongside a running (center, right) pair to skip work via mirror reflection. Linear time. manacher "babadaba" = 7 (* witness: "abadaba", positions 1..7 *) Note: requires parenthesizing the if-expression on the rhs of <-: p.(i) <- (if pm < v then pm else v) Real OCaml parses bare `if` at <-rhs since the rhs is at expr level; our parser places <-rhs at binop level which doesn't include `if` / `match` / `let`. Workaround until we relax the binop RHS grammar. 149 baseline programs total.
This commit is contained in:
@@ -87,6 +87,7 @@
|
||||
"lambda_calc.ml": 7,
|
||||
"lcs.ml": 4,
|
||||
"majority_vote.ml": 4,
|
||||
"manacher.ml": 7,
|
||||
"levenshtein.ml": 11,
|
||||
"memo_fib.ml": 75025,
|
||||
"mortgage.ml": 1073,
|
||||
|
||||
32
lib/ocaml/baseline/manacher.ml
Normal file
32
lib/ocaml/baseline/manacher.ml
Normal file
@@ -0,0 +1,32 @@
|
||||
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"
|
||||
Reference in New Issue
Block a user