Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (*Najdłuższy wspólny podciąg*)
- open Array
- let nwp s1 s2 =
- let tab = make_matrix ((length s2) + 1) ((length s1) + 1) 0 in
- for i=0 to length s1 - 1 do
- for j=0 to length s2 - 1 do
- if s1.(i) = s2.(j)
- then tab.(j+1).(i+1) <- tab.(j).(i) + 1
- else tab.(j+1).(i+1) <- max tab.(j).(i+1) tab.(j+1).(i)
- done;
- done;
- tab;;
- let odzyskaj s1 s2 =
- let odp = ref []
- and tab = (nwp s1 s2)
- and i = ref (length s1)
- and j = ref (length s2) in
- while (!i != 0) && (!j != 0) do
- if tab.(!j-1).(!i) < tab.(!j).(!i) && tab.(!j).(!i-1) < tab.(!j).(!i)
- then (
- odp := (s1.(!i-1))::(!odp);
- decr i;
- decr j
- )
- else (
- if tab.(!j-1).(!i) = tab.(!j).(!i)
- then (decr j;)
- else (decr i;)
- );
- done;
- !odp;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement