mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-07 10:00:55 +00:00
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
a4073ca188
commit
8dcffa3e71
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
import doctest
|
import doctest
|
||||||
|
|
||||||
|
|
||||||
def build_suffix_array(input_string: str) -> list[int]:
|
def build_suffix_array(input_string: str) -> list[int]:
|
||||||
"""
|
"""
|
||||||
Build the suffix array for the given string.
|
Build the suffix array for the given string.
|
||||||
|
@ -22,6 +23,7 @@ def build_suffix_array(input_string: str) -> list[int]:
|
||||||
suffix_array = [suffix[1] for suffix in suffixes]
|
suffix_array = [suffix[1] for suffix in suffixes]
|
||||||
return suffix_array
|
return suffix_array
|
||||||
|
|
||||||
|
|
||||||
def build_lcp_array(input_string: str, suffix_array: list[int]) -> list[int]:
|
def build_lcp_array(input_string: str, suffix_array: list[int]) -> list[int]:
|
||||||
"""
|
"""
|
||||||
Build the LCP array for the given string and suffix array.
|
Build the LCP array for the given string and suffix array.
|
||||||
|
@ -51,13 +53,18 @@ def build_lcp_array(input_string: str, suffix_array: list[int]) -> list[int]:
|
||||||
for i in range(n):
|
for i in range(n):
|
||||||
if rank[i] > 0:
|
if rank[i] > 0:
|
||||||
j = suffix_array[rank[i] - 1]
|
j = suffix_array[rank[i] - 1]
|
||||||
while (i + h < n) and (j + h < n) and (input_string[i + h] == input_string[j + h]):
|
while (
|
||||||
|
(i + h < n)
|
||||||
|
and (j + h < n)
|
||||||
|
and (input_string[i + h] == input_string[j + h])
|
||||||
|
):
|
||||||
h += 1
|
h += 1
|
||||||
lcp[rank[i]] = h
|
lcp[rank[i]] = h
|
||||||
if h > 0:
|
if h > 0:
|
||||||
h -= 1 # Decrease h for the next suffix
|
h -= 1 # Decrease h for the next suffix
|
||||||
return lcp
|
return lcp
|
||||||
|
|
||||||
|
|
||||||
# Example usage
|
# Example usage
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
s = "banana"
|
s = "banana"
|
||||||
|
@ -70,8 +77,10 @@ if __name__ == "__main__":
|
||||||
|
|
||||||
print("\nLCP Array:")
|
print("\nLCP Array:")
|
||||||
for i in range(1, len(lcp_array)):
|
for i in range(1, len(lcp_array)):
|
||||||
lcp_info = (f"LCP between {s[suffix_array[i - 1]:]} and "
|
lcp_info = (
|
||||||
f"{s[suffix_array[i]]}: {lcp_array[i]}")
|
f"LCP between {s[suffix_array[i - 1]:]} and "
|
||||||
|
f"{s[suffix_array[i]]}: {lcp_array[i]}"
|
||||||
|
)
|
||||||
print(lcp_info)
|
print(lcp_info)
|
||||||
|
|
||||||
# Run doctests
|
# Run doctests
|
||||||
|
|
Loading…
Reference in New Issue
Block a user