Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- open Hashtbl
- let fib n=
- let wynik = Hashtbl.create n in
- let rec pom a b k =
- if k<=n then
- begin
- Hashtbl.add wynik b k;
- pom b (a+b) (k+1);
- end;
- in
- begin
- Hashtbl.add wynik 1 1;
- pom 1 1 2;
- end;
- wynik
- let abs x =
- if x<0 then -x
- else x
- let pred a b =
- if abs(a-b)=1 then true
- else if(a=2) then if abs(1-b)=1 then true
- else false
- else if(b=2) then if abs(1-a)=1 then true
- else false
- else false
- let decision a b = if pred a b then max a b +1
- else -1
- let segment tab =
- let maxi = ref 0 in
- let n = Array.length tab in
- let fibtab = fib n in
- let dp = Array.make_matrix n n 0 in
- for i = 0 to (n-1) do
- let w = Hashtbl.find_opt fibtab tab.(i) in
- match w with
- |None -> (dp.(i).(i) <- (-1);)
- |Some w -> (dp.(i).(i) <- (w);)
- done;
- for x = 1 to n-1
- do
- for i=0 to n-1-x
- do
- dp.(i).(i+x) <- decision dp.(i).(i+x-1) dp.(i+x).(i+x) ;
- let cur = dp.(i).(i+x) in
- if cur > !maxi then maxi:= cur;
- done;
- done;
- !maxi
Add Comment
Please, Sign In to add comment