ocaml: phase 5.1 dp_word_break.ml baseline (4/5 strings segmentable)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
Classic word-break DP — for each position i, check whether any
dictionary word ends at i with a prior reachable position:
dp[i] = exists w in dict with wl <= i and
dp[i - wl] && s.sub (i - wl) wl = w
Dictionary: apple, pen, pine, pineapple, cats, cat, and, sand, dog
Inputs:
applepenapple yes (apple pen apple)
pineapplepenapple yes (pineapple pen apple)
catsanddog yes (cats and dog)
catsandog no (no segmentation reaches the end)
applesand yes (apple sand)
Tests bool-typed Array, String.sub primitive, nested List.iter
over the dict inside for-loop over end positions, closure capture
of the outer dp.
179 baseline programs total.
This commit is contained in:
27
lib/ocaml/baseline/dp_word_break.ml
Normal file
27
lib/ocaml/baseline/dp_word_break.ml
Normal file
@@ -0,0 +1,27 @@
|
||||
let word_break s words =
|
||||
let n = String.length s in
|
||||
let dp = Array.make (n + 1) false in
|
||||
dp.(0) <- true;
|
||||
for i = 1 to n do
|
||||
List.iter (fun w ->
|
||||
let wl = String.length w in
|
||||
if i >= wl && dp.(i - wl) then begin
|
||||
let prefix = String.sub s (i - wl) wl in
|
||||
if prefix = w then dp.(i) <- true
|
||||
end
|
||||
) words
|
||||
done;
|
||||
if dp.(n) then 1 else 0
|
||||
|
||||
let count_ok strings words =
|
||||
let count = ref 0 in
|
||||
List.iter (fun s ->
|
||||
count := !count + word_break s words
|
||||
) strings;
|
||||
!count
|
||||
|
||||
;;
|
||||
|
||||
let dict = ["apple"; "pen"; "pine"; "pineapple"; "cats"; "cat"; "and"; "sand"; "dog"] in
|
||||
let inputs = ["applepenapple"; "pineapplepenapple"; "catsanddog"; "catsandog"; "applesand"] in
|
||||
count_ok inputs dict
|
||||
@@ -35,6 +35,7 @@
|
||||
"csv.ml": 10,
|
||||
"egg_drop.ml": 8,
|
||||
"dijkstra.ml": 7,
|
||||
"dp_word_break.ml": 4,
|
||||
"distinct_subseq.ml": 3,
|
||||
"exception_handle.ml": 4,
|
||||
"exception_user.ml": 26,
|
||||
|
||||
Reference in New Issue
Block a user