Commit e9c57cd6 authored by MARCHE Claude's avatar MARCHE Claude

lcp: syntax OK until tests

parent 8619672a
......@@ -31,7 +31,7 @@ Pseudocode:
ADVANCED
ADVANCED
========
......@@ -53,7 +53,7 @@ Typically, the suffixes are not stored explicitly as above but
represented as pointers into the original text. The suffixes in a suffix
array are sorted in lexicographical order. This way, occurrences of
repeated substrings in the original text are neighbors in the suffix
array.
array.
For the above, example (assuming pointers are 0-based integers), the
sorted suffix array is:
......@@ -74,60 +74,56 @@ that it does so correctly.
*)
module SuffixArray
module SuffixArray
use import int.Int
use import array.Array
type suffixArray = {
a : map int int;
suffixes : map int int;
n : int;
values : array int;
suffixes : array int;
}
let create (a:map int int) (length:int) : suffixArray =
let s = Array.make 0 length in
for i = 0 to length-1 do s[i] <- i done;
sort(s);
{ a = a;
n = length;
suffixes = s.elts;
}
let select (s:suffixArray) (int i) : int = s.suffixes[i]
let private_lcp (s:suffixArray) (x y:int) : int =
let n = s.n in
let select (s:suffixArray) (i:int) : int = s.suffixes[i]
use import ref.Refint
let lcp (a:array int) (x y:int) : int =
let n = a.length in
let l = ref 0 in
while x + !l < n && y + !l < n && s.a[x + !l] = a[y + !l] do
while x + !l < n && y + !l < n && a[x + !l] = a[y + !l] do
incr l
done;
!l
let lcp (s:suffixArray) (i:int) : int =
private_lcp s.suffixes[i] s.suffixes[i-1]
let compare (s:suffixArray) (x y:int) : int =
let compare (a:array int) (x y:int) : int =
if x = y then 0 else
let l = ref 0 in
while x + !l < n && y + !l < n && s.a[x + !l] = s.a[y + !l] do
incr l
done;
if x + !l = n then -1 else
if y + !l = n then 1 else
if s.a[x + !l] < s.a[y + !l] then -1 else
if s.a[x + !l] > s.a[y + !l] then 1 else
let n = a.length in
let l = lcp a x y in
if x + l = n then -1 else
if y + l = n then 1 else
if a[x + l] < a[y + l] then -1 else
if a[x + l] > a[y + l] then 1 else
absurd
let sort (s:suffixArray) (data : array int) =
for i = 0 to data.length do
let sort (a:array int) (data : array int) =
for i = 0 to data.length - 1 do
let j = ref i in
while j > 0 && compare s data[!j-1] data[!j] > 0 do
let b = !j - 1 in
let t = data[!j] in
data[!j] <- data[b];
data[b] <- t;
decr j
while !j > 0 && compare a data[!j-1] data[!j] > 0 do
let b = !j - 1 in
let t = data[!j] in
data[!j] <- data[b];
data[b] <- t;
decr j
done
done
let create (a:array int) : suffixArray =
let n = a.length in
let suf = Array.make 0 n in
for i = 0 to n-1 do suf[i] <- i done;
sort a suf;
{ values = a; suffixes = suf }
let test1 () =
int[] arr = {1,2,2,5};
......@@ -149,7 +145,11 @@ let test1 () =
end
module LRS
module LRS
let lcp (s:suffixArray) (i:int) : int =
SuffixArray.lcp s s.suffixes[i] s.suffixes[i-1]
private static int solStart = 0;
private static int solLength = 0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment