step20
Loading...
Searching...
No Matches
longest_repeated_substring.hpp
Go to the documentation of this file.
1// Andrew Naplavkov
2
3#ifndef STEP20_LONGEST_REPEATED_SUBSTRING_HPP
4#define STEP20_LONGEST_REPEATED_SUBSTRING_HPP
5
6#include "suffix_array.hpp"
7#include "suffix_tree.hpp"
8
9namespace step20 {
10
12
15template <class... Ts>
17 -> std::basic_string_view<typename enhanced_suffix_array<Ts...>::value_type>
18{
19 auto first = arr.data();
20 auto last = first + arr.size();
21 auto result = std::basic_string_view{last, last};
22 auto lcp = arr.lcp_array();
23 auto max = std::ranges::max_element(lcp);
24 if (max != lcp.end() && *max > 0) {
25 auto pos = arr[max - lcp.begin()];
26 result = std::basic_string_view{first + pos, first + pos + *max};
27 }
28 return result;
29}
30
32
37template <class... Ts>
39 -> std::basic_string_view<typename suffix_tree<Ts...>::value_type>
40{
41 auto first = tree.data();
42 auto last = first + tree.size();
43 auto result = std::basic_string_view{last, last};
44 if (auto start = tree.find(result))
45 for (auto edge : tree.depth_first_search(*start)) {
46 if (tree.leaf(edge.child_node) || edge.labels_len <= result.size())
47 continue;
48 auto labels = tree.labels(edge);
49 result = std::basic_string_view{first + labels.first,
50 first + labels.last};
51 }
52 return result;
53}
54
55} // namespace step20
56
57#endif // STEP20_LONGEST_REPEATED_SUBSTRING_HPP
Kasai's algorithm for constructing longest common prefix array.
Definition suffix_array.hpp:122
const Char * data() const
Definition suffix_array.hpp:27
Ukkonen's online algorithm for constructing suffix tree.
Definition suffix_tree.hpp:24
const Char * data() const
Definition suffix_tree.hpp:28
Definition edit_distance.hpp:11
auto longest_repeated_substring(const enhanced_suffix_array< Ts... > &arr) -> std::basic_string_view< typename enhanced_suffix_array< Ts... >::value_type >
Find the longest substring that occurs at least twice.
Definition longest_repeated_substring.hpp:16