ocaml: phase 5.1 distinct_subseq.ml baseline ("rabbit" in "rabbbit" = 3)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 21s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 21s
Classic distinct-subsequences 2D DP: dp[i][j] = dp[i-1][j] + (s[i-1] = t[j-1] ? dp[i-1][j-1] : 0) dp[i][0] = 1 (empty t is a subseq of any prefix of s) count_subseq "rabbbit" "rabbit" = 3 The three witnesses correspond to which 'b' in "rabbbit" is dropped (positions 2, 3, or 4 zero-indexed of the run of bs). Complements subseq_check.ml (just tests presence); this one counts distinct embeddings. Tests 2D DP with Array.init n (fun _ -> Array.make m 0), base row initialization, mixed string + array indexing. 175 baseline programs total.
This commit is contained in:
20
lib/ocaml/baseline/distinct_subseq.ml
Normal file
20
lib/ocaml/baseline/distinct_subseq.ml
Normal file
@@ -0,0 +1,20 @@
|
||||
let count_subseq s t =
|
||||
let m = String.length s in
|
||||
let n = String.length t in
|
||||
let dp = Array.init (m + 1) (fun _ -> Array.make (n + 1) 0) in
|
||||
for i = 0 to m do
|
||||
dp.(i).(0) <- 1
|
||||
done;
|
||||
for i = 1 to m do
|
||||
for j = 1 to n do
|
||||
if s.[i - 1] = t.[j - 1] then
|
||||
dp.(i).(j) <- dp.(i - 1).(j) + dp.(i - 1).(j - 1)
|
||||
else
|
||||
dp.(i).(j) <- dp.(i - 1).(j)
|
||||
done
|
||||
done;
|
||||
dp.(m).(n)
|
||||
|
||||
;;
|
||||
|
||||
count_subseq "rabbbit" "rabbit"
|
||||
@@ -34,6 +34,7 @@
|
||||
"csv.ml": 10,
|
||||
"egg_drop.ml": 8,
|
||||
"dijkstra.ml": 7,
|
||||
"distinct_subseq.ml": 3,
|
||||
"exception_handle.ml": 4,
|
||||
"exception_user.ml": 26,
|
||||
"euler1.ml": 233168,
|
||||
|
||||
Reference in New Issue
Block a user