Skip to content

Commit 0457860

Browse files
authored
Suffix Array and LCP Array Implementation
1 parent 03a4251 commit 0457860

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed
+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env python3
2+
3+
def build_suffix_array(s: str) -> list[int]:
4+
"""
5+
Build the suffix array for the given string.
6+
7+
Parameters:
8+
s (str): The input string.
9+
10+
Returns:
11+
list[int]: The suffix array (a list of starting indices of
12+
suffixes in sorted order).
13+
"""
14+
suffixes = [(s[i:], i) for i in range(len(s))]
15+
suffixes.sort() # Sort the suffixes lexicographically
16+
suffix_array = [suffix[1] for suffix in suffixes]
17+
return suffix_array
18+
19+
def build_lcp_array(s: str, suffix_array: list[int]) -> list[int]:
20+
"""
21+
Build the LCP array for the given string and suffix array.
22+
23+
Parameters:
24+
s (str): The input string.
25+
suffix_array (list[int]): The suffix array.
26+
27+
Returns:
28+
list[int]: The LCP array.
29+
"""
30+
n = len(s)
31+
rank = [0] * n
32+
lcp = [0] * n
33+
34+
# Compute the rank of each suffix
35+
for i, suffix_index in enumerate(suffix_array):
36+
rank[suffix_index] = i
37+
38+
# Compute the LCP array
39+
h = 0
40+
for i in range(n):
41+
if rank[i] > 0:
42+
j = suffix_array[rank[i] - 1]
43+
while (i + h < n) and (j + h < n) and (s[i + h] == s[j + h]):
44+
h += 1
45+
lcp[rank[i]] = h
46+
if h > 0:
47+
h -= 1 # Decrease h for the next suffix
48+
return lcp
49+
50+
# Example usage
51+
if __name__ == "__main__":
52+
s = "banana"
53+
suffix_array = build_suffix_array(s)
54+
lcp_array = build_lcp_array(s, suffix_array)
55+
56+
print("Suffix Array:")
57+
for i in range(len(suffix_array)):
58+
print(f"{suffix_array[i]}: {s[suffix_array[i]:]}")
59+
60+
print("\nLCP Array:")
61+
for i in range(1, len(lcp_array)):
62+
print(f"LCP between {s[suffix_array[i - 1]:]} and {s[suffix_array[i]]}: {lcp_array[i]}")

0 commit comments

Comments
 (0)