m1une's library

This documentation is automatically generated by online-judge-tools/verification-helper

View on GitHub

:heavy_check_mark: verify/string/suffix_tree.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/number_of_substrings"

#include "../../string/suffix_tree.hpp"

#include "../../utilities/fast_io.hpp"
#include <algorithm>
#include <cassert>
#include <cstdint>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>

namespace {

void test_features() {
    using Tree = m1une::string::SuffixTree<>;
    Tree empty;
    assert(empty.empty());
    assert(empty.text_length() == 0);
    assert(empty.node_count() == 2);
    assert(empty.count_occurrences(std::string()) == 1);
    assert(empty.distinct_substring_count() == 0);

    std::string text = "ababa";
    Tree tree(text);
    assert(!tree.empty());
    assert(tree.text_length() == 5);
    assert(tree.root() == Tree::root_node);
    assert(tree.node_count() <= 2 * int(text.size()) + 1);
    assert(tree.contains(std::string("bab")));
    assert(!tree.contains(std::string("abb")));
    assert(tree.contains(std::string()));
    assert(tree.count_occurrences(std::string("a")) == 3);
    assert(tree.count_occurrences(std::string("aba")) == 2);
    assert(tree.count_occurrences(std::string("ababa")) == 1);
    assert(tree.count_occurrences(std::string()) == 6);
    assert(tree.distinct_substring_count() == 9);

    auto occurrence = tree.representative_occurrence(std::string("bab"));
    assert(text.substr(occurrence.first, occurrence.second - occurrence.first) == "bab");
    assert((tree.representative_occurrence(std::string("abb")) == std::pair<int, int>(-1, -1)));

    int root_children = 0;
    bool has_terminal_leaf = false;
    tree.for_each_child(tree.root(), [&](int symbol, int child) {
        root_children++;
        assert(tree.node(child).parent == tree.root());
        if (symbol == Tree::terminal_symbol) {
            has_terminal_leaf = true;
            assert(tree.is_leaf(child));
            assert(tree.node(child).suffix_start == int(text.size()));
        }
    });
    assert(root_children == 3);
    assert(has_terminal_leaf);
    assert(tree.child(tree.root(), 'a') != Tree::null_node);
    assert(tree.child_by_index(tree.root(), Tree::terminal_symbol) != Tree::null_node);

    m1une::string::SuffixTree<10, '0'> digits(std::string("012012"));
    assert(digits.contains(std::string("201")));

    tree.clear();
    assert(tree.empty());
    assert(tree.node_count() == 2);
}

void test_randomized() {
    std::uint64_t state = 918273645;
    auto random = [&state]() {
        state ^= state << 7;
        state ^= state >> 9;
        return state;
    };

    for (int trial = 0; trial < 3000; trial++) {
        int n = int(random() % 55);
        std::string text(n, 'a');
        for (char& character : text) character = char('a' + random() % 4);
        m1une::string::SuffixTree<4, 'a'> tree(text);

        assert(tree.node_count() <= std::max(2, 2 * n + 1));
        assert(tree.node(tree.root()).parent == tree.null_node);
        assert(tree.node(tree.root()).suffix_link == tree.root());

        std::map<std::string, int> expected;
        for (int left = 0; left < n; left++) {
            for (int right = left + 1; right <= n; right++) {
                expected[text.substr(left, right - left)]++;
            }
        }
        assert(tree.distinct_substring_count() == int(expected.size()));
        assert(tree.count_occurrences(std::string()) == n + 1);

        for (const auto& entry : expected) {
            const std::string& substring = entry.first;
            int count = entry.second;
            assert(tree.contains(substring));
            assert(tree.count_occurrences(substring) == count);
            auto [left, right] = tree.representative_occurrence(substring);
            assert(0 <= left && left < right && right <= n);
            assert(text.substr(left, right - left) == substring);
        }

        std::set<int> suffix_starts;
        int leaves = 0;
        for (int id = 0; id < tree.node_count(); id++) {
            const auto& node = tree.node(id);
            assert(0 <= node.left && node.left <= node.right && node.right <= n + 1);
            if (id != tree.root()) {
                assert(0 <= node.parent && node.parent < tree.node_count());
                assert(tree.edge_length(id) > 0);
            }
            if (tree.is_leaf(id)) {
                leaves++;
                assert(0 <= node.suffix_start && node.suffix_start <= n);
                suffix_starts.insert(node.suffix_start);
                assert(node.leaf_count == 1);
            } else {
                assert(node.suffix_start == -1);
                assert(node.leaf_count >= (id == tree.root() ? 1 : 2));
            }

            std::vector<std::pair<int, int>> children;
            tree.for_each_child(id, [&](int symbol, int child) {
                children.emplace_back(symbol, child);
            });
            assert(int(children.size()) == node.child_count);

            int position = 0;
            for (int child = node.first_child; child != tree.null_node; child = tree.node(child).next_sibling) {
                assert(position < int(children.size()));
                assert(tree.node(child).parent == id);
                assert(tree.node(child).incoming_symbol == children[position].first);
                assert(child == children[position].second);
                assert(tree.child_by_index(id, children[position].first) == child);
                position++;
            }
            assert(position == int(children.size()));
            for (int i = 1; i < int(children.size()); i++) {
                assert(children[i - 1].first < children[i].first);
            }
        }
        assert(leaves == n + 1);
        assert(int(suffix_starts.size()) == n + 1);
        for (int start = 0; start <= n; start++) assert(suffix_starts.count(start));

        for (int length = 0; length <= n; length++) {
            std::string suffix = text.substr(n - length);
            assert(tree.contains(suffix));
            assert(tree.count_occurrences(suffix) >= 1);
        }

        int query_length = int(random() % 25);
        std::string query(query_length, 'a');
        for (char& character : query) character = char('a' + random() % 4);
        int naive_count = 0;
        for (int position = 0; position + query_length <= n; position++) {
            if (text.compare(position, query_length, query) == 0) naive_count++;
        }
        if (query.empty()) naive_count = n + 1;
        assert(tree.count_occurrences(query) == naive_count);
    }
}

}  // namespace

int main() {
    m1une::utilities::FastInput fast_input;
    m1une::utilities::FastOutput fast_output;

    test_features();
    test_randomized();

    std::string text;
    fast_input >> text;
    m1une::string::SuffixTree<> tree(text);
    fast_output << tree.distinct_substring_count() << '\n';
}
#line 1 "verify/string/suffix_tree.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/number_of_substrings"

#line 1 "string/suffix_tree.hpp"



#include <algorithm>
#include <array>
#include <cassert>
#include <cstddef>
#include <limits>
#include <utility>
#include <vector>

namespace m1une {
namespace string {

template <int AlphabetSize = 26, int FirstCharacter = 'a'>
struct SuffixTree {
    static_assert(0 < AlphabetSize);

    using node_id = int;
    static constexpr node_id root_node = 0;
    static constexpr node_id null_node = -1;
    static constexpr int terminal_symbol = AlphabetSize;

    struct Node {
        std::array<node_id, AlphabetSize + 1> next;
        node_id suffix_link;
        node_id parent;
        int left;
        int right;
        int suffix_start;
        int representative_suffix;
        int leaf_count;
        int incoming_symbol;
        node_id first_child;
        node_id next_sibling;
        int child_count;

        Node(int left_value = 0, int right_value = 0, node_id parent_value = null_node)
            : suffix_link(null_node),
              parent(parent_value),
              left(left_value),
              right(right_value),
              suffix_start(-1),
              representative_suffix(-1),
              leaf_count(0),
              incoming_symbol(-1),
              first_child(null_node),
              next_sibling(null_node),
              child_count(0) {
            next.fill(null_node);
        }
    };

    struct Locus {
        node_id node;
        int offset;

        explicit operator bool() const {
            return node != null_node;
        }

        friend bool operator==(const Locus&, const Locus&) = default;
    };

   private:
    struct ActivePoint {
        node_id node;
        int offset;
    };

    std::vector<Node> _nodes;
    std::vector<int> _text;
    ActivePoint _active;
    int _text_length;

    template <class Symbol>
    static int symbol_index(const Symbol& symbol) {
        int index = int(symbol) - FirstCharacter;
        assert(0 <= index && index < AlphabetSize);
        return index;
    }

    int edge_length_unchecked(node_id id) const {
        return _nodes[id].right - _nodes[id].left;
    }

    node_id new_node(int left, int right, node_id parent) {
        assert(_nodes.size() < std::size_t(std::numeric_limits<int>::max()));
        _nodes.emplace_back(left, right, parent);
        return int(_nodes.size()) - 1;
    }

    ActivePoint go(ActivePoint point, int left, int right) const {
        while (left < right) {
            if (point.offset == edge_length_unchecked(point.node)) {
                point = {_nodes[point.node].next[_text[left]], 0};
                if (point.node == null_node) return point;
            } else {
                if (_text[_nodes[point.node].left + point.offset] != _text[left]) {
                    return {null_node, 0};
                }
                int remaining = edge_length_unchecked(point.node) - point.offset;
                if (right - left < remaining) {
                    point.offset += right - left;
                    return point;
                }
                left += remaining;
                point.offset = edge_length_unchecked(point.node);
            }
        }
        return point;
    }

    node_id split(ActivePoint point) {
        if (point.offset == edge_length_unchecked(point.node)) return point.node;
        if (point.offset == 0) return _nodes[point.node].parent;

        node_id child = point.node;
        node_id parent = _nodes[child].parent;
        int left = _nodes[child].left;
        node_id middle = new_node(left, left + point.offset, parent);
        _nodes[parent].next[_text[left]] = middle;
        _nodes[middle].next[_text[left + point.offset]] = child;
        _nodes[child].parent = middle;
        _nodes[child].left += point.offset;
        return middle;
    }

    node_id get_suffix_link(node_id id) {
        if (_nodes[id].suffix_link != null_node) return _nodes[id].suffix_link;
        node_id parent = _nodes[id].parent;
        if (parent == null_node) return root_node;

        node_id parent_link = get_suffix_link(parent);
        ActivePoint point = {
            parent_link,
            edge_length_unchecked(parent_link)
        };
        int left = _nodes[id].left + (parent == root_node);
        point = go(point, left, _nodes[id].right);
        assert(point.node != null_node);
        return _nodes[id].suffix_link = split(point);
    }

    void extend(int position) {
        while (true) {
            ActivePoint next = go(_active, position, position + 1);
            if (next.node != null_node) {
                _active = next;
                return;
            }

            node_id middle = split(_active);
            node_id leaf = new_node(position, int(_text.size()), middle);
            _nodes[middle].next[_text[position]] = leaf;

            _active.node = get_suffix_link(middle);
            _active.offset = edge_length_unchecked(_active.node);
            if (middle == root_node) return;
        }
    }

    void finish_metadata() {
        std::vector<node_id> order;
        order.reserve(_nodes.size());
        order.push_back(root_node);
        std::vector<int> depth(_nodes.size(), 0);

        for (std::size_t i = 0; i < order.size(); i++) {
            node_id id = order[i];
            node_id previous_child = null_node;
            for (int symbol = 0; symbol <= terminal_symbol; symbol++) {
                node_id child = _nodes[id].next[symbol];
                if (child == null_node) continue;
                _nodes[child].incoming_symbol = symbol;
                if (previous_child == null_node) {
                    _nodes[id].first_child = child;
                } else {
                    _nodes[previous_child].next_sibling = child;
                }
                previous_child = child;
                _nodes[id].child_count++;
                depth[child] = depth[id] + edge_length_unchecked(child);
                order.push_back(child);
            }
        }

        for (int i = int(order.size()) - 1; i >= 0; i--) {
            node_id id = order[i];
            bool leaf = true;
            for (node_id child : _nodes[id].next) {
                if (child == null_node) continue;
                leaf = false;
                _nodes[id].leaf_count += _nodes[child].leaf_count;
                if (_nodes[id].representative_suffix == -1) {
                    _nodes[id].representative_suffix = _nodes[child].representative_suffix;
                }
            }
            if (leaf) {
                _nodes[id].suffix_start = int(_text.size()) - depth[id];
                _nodes[id].representative_suffix = _nodes[id].suffix_start;
                _nodes[id].leaf_count = 1;
            }
        }
    }

    void initialize() {
        _nodes.clear();
        _nodes.reserve(2 * _text.size() + 1);
        _nodes.emplace_back();
        _nodes[root_node].suffix_link = root_node;
        _active = {root_node, 0};
        for (int position = 0; position < int(_text.size()); position++) extend(position);
        finish_metadata();
    }

   public:
    SuffixTree() {
        clear();
    }

    template <class Sequence>
    explicit SuffixTree(const Sequence& sequence) {
        build(sequence);
    }

    int size() const {
        return node_count();
    }

    bool empty() const {
        return _text_length == 0;
    }

    int node_count() const {
        return int(_nodes.size());
    }

    int text_length() const {
        return _text_length;
    }

    node_id root() const {
        return root_node;
    }

    const Node& node(node_id id) const {
        assert(0 <= id && id < node_count());
        return _nodes[id];
    }

    const std::vector<Node>& nodes() const {
        return _nodes;
    }

    int edge_length(node_id id) const {
        assert(0 <= id && id < node_count());
        return edge_length_unchecked(id);
    }

    bool is_leaf(node_id id) const {
        assert(0 <= id && id < node_count());
        return _nodes[id].suffix_start != -1;
    }

    template <class Symbol>
    node_id child(node_id id, const Symbol& symbol) const {
        assert(0 <= id && id < node_count());
        return _nodes[id].next[symbol_index(symbol)];
    }

    node_id child_by_index(node_id id, int symbol) const {
        assert(0 <= id && id < node_count());
        assert(0 <= symbol && symbol <= terminal_symbol);
        return _nodes[id].next[symbol];
    }

    template <class Callback>
    void for_each_child(node_id id, Callback callback) const {
        assert(0 <= id && id < node_count());
        for (
            node_id child_id = _nodes[id].first_child;
            child_id != null_node;
            child_id = _nodes[child_id].next_sibling
        ) {
            callback(_nodes[child_id].incoming_symbol, child_id);
        }
    }

    void clear() {
        _text.clear();
        _text.push_back(terminal_symbol);
        _text_length = 0;
        initialize();
    }

    template <class Sequence>
    void build(const Sequence& sequence) {
        _text.clear();
        for (const auto& symbol : sequence) _text.push_back(symbol_index(symbol));
        assert(_text.size() < std::size_t(std::numeric_limits<int>::max()));
        _text_length = int(_text.size());
        _text.push_back(terminal_symbol);
        initialize();
    }

    template <class Sequence>
    Locus find(const Sequence& sequence) const {
        ActivePoint point = {root_node, 0};
        for (const auto& value : sequence) {
            int symbol = symbol_index(value);
            if (point.offset == edge_length_unchecked(point.node)) {
                point = {_nodes[point.node].next[symbol], 0};
                if (point.node == null_node) return {null_node, 0};
            }
            if (_text[_nodes[point.node].left + point.offset] != symbol) {
                return {null_node, 0};
            }
            point.offset++;
        }
        return {point.node, point.offset};
    }

    template <class Sequence>
    bool contains(const Sequence& sequence) const {
        return bool(find(sequence));
    }

    template <class Sequence>
    int count_occurrences(const Sequence& sequence) const {
        Locus locus = find(sequence);
        return locus ? _nodes[locus.node].leaf_count : 0;
    }

    template <class Sequence>
    std::pair<int, int> representative_occurrence(const Sequence& sequence) const {
        Locus locus = {root_node, 0};
        int length = 0;
        for (const auto& value : sequence) {
            int symbol = symbol_index(value);
            if (locus.offset == edge_length_unchecked(locus.node)) {
                locus = {_nodes[locus.node].next[symbol], 0};
                if (locus.node == null_node) return {-1, -1};
            }
            if (_text[_nodes[locus.node].left + locus.offset] != symbol) return {-1, -1};
            locus.offset++;
            length++;
        }
        int left = _nodes[locus.node].representative_suffix;
        return {left, left + length};
    }

    long long distinct_substring_count() const {
        long long result = 0;
        for (node_id id = 1; id < node_count(); id++) {
            result += std::max(0, std::min(_nodes[id].right, _text_length) - _nodes[id].left);
        }
        return result;
    }
};

}  // namespace string
}  // namespace m1une


#line 4 "verify/string/suffix_tree.test.cpp"

#line 1 "utilities/fast_io.hpp"



#line 6 "utilities/fast_io.hpp"
#include <cerrno>
#include <charconv>
#line 9 "utilities/fast_io.hpp"
#include <cstdio>
#include <cstdlib>
#include <cstdint>
#include <cstring>
#include <iterator>
#include <string>
#include <sys/stat.h>
#include <type_traits>
#line 18 "utilities/fast_io.hpp"
#include <unistd.h>
#line 20 "utilities/fast_io.hpp"

namespace m1une {
namespace utilities {

struct FastOutput;

namespace internal {

// Shared with the convenience helpers in template.hpp.
inline FastOutput* standard_output_instance = nullptr;

// Detect std::begin(x), std::end(x).
template <class T, class = void>
struct is_range : std::false_type {};

template <class T>
struct is_range<T, std::void_t<
    decltype(std::begin(std::declval<T&>())),
    decltype(std::end(std::declval<T&>()))
>> : std::true_type {};

template <class T>
inline constexpr bool is_range_v = is_range<T>::value;

template <class T>
using range_reference_t = decltype(*std::begin(std::declval<T&>()));

template <class T>
using range_value_t = std::remove_cv_t<std::remove_reference_t<range_reference_t<T>>>;

template <class T, class = void>
struct range_stored_value {
    using type = range_value_t<T>;
};

template <class T>
struct range_stored_value<T, std::void_t<typename std::remove_cv_t<std::remove_reference_t<T>>::value_type>> {
    using type = typename std::remove_cv_t<std::remove_reference_t<T>>::value_type;
};

template <class T>
using range_stored_value_t = typename range_stored_value<T>::type;

// Treat strings and C strings as scalar output objects, not as ranges.
template <class T>
struct is_char_array : std::false_type {};

template <class T, std::size_t N>
struct is_char_array<T[N]>
    : std::bool_constant<std::is_same_v<std::remove_cv_t<T>, char>> {};

template <class T>
struct is_string_like
    : std::bool_constant<
          std::is_same_v<std::decay_t<T>, std::string>
          || std::is_same_v<std::decay_t<T>, const char*>
          || std::is_same_v<std::decay_t<T>, char*>
          || is_char_array<std::remove_reference_t<T>>::value
      > {};

template <class T>
inline constexpr bool is_string_like_v = is_string_like<T>::value;

// ModInt-like type: x.val() is printable, and x can be assigned from long long.
template <class T, class = void>
struct has_val_method : std::false_type {};

template <class T>
struct has_val_method<T, std::void_t<decltype(std::declval<const T&>().val())>>
    : std::true_type {};

template <class T>
inline constexpr bool has_val_method_v = has_val_method<T>::value;

template <class T, class = void>
struct has_static_mod_raw : std::false_type {};

template <class T>
struct has_static_mod_raw<
    T, std::void_t<decltype(T::mod()), decltype(T::raw(std::declval<uint32_t>()))>>
    : std::true_type {};

template <class T>
inline constexpr bool has_static_mod_raw_v = has_static_mod_raw<T>::value;

// libstdc++ before GCC 16 does not classify __int128 as an integral type in
// strict ISO modes such as -std=c++23. Keep the fast-I/O interface independent
// of that implementation detail.
template <class T>
inline constexpr bool is_integral_v =
    std::is_integral_v<T>
    || std::is_same_v<std::remove_cv_t<T>, __int128_t>
    || std::is_same_v<std::remove_cv_t<T>, __uint128_t>;

template <class T>
inline constexpr bool is_signed_v =
    std::is_signed_v<T>
    || std::is_same_v<std::remove_cv_t<T>, __int128_t>;

template <class T>
struct make_unsigned {
    using type = std::make_unsigned_t<T>;
};

template <>
struct make_unsigned<__int128_t> {
    using type = __uint128_t;
};

template <>
struct make_unsigned<__uint128_t> {
    using type = __uint128_t;
};

template <class T>
using make_unsigned_t = typename make_unsigned<std::remove_cv_t<T>>::type;

}  // namespace internal

struct FastInput {
    static constexpr int buffer_size = 1 << 20;

   private:
    std::FILE* _stream;
    char _buffer[buffer_size];
    int _position;
    int _length;
    int _file_descriptor;
    bool _streaming;

    bool refill() {
        _position = 0;
        if (_streaming) {
            ssize_t length;
            do {
                length = ::read(_file_descriptor, _buffer, buffer_size);
            } while (length < 0 && errno == EINTR);
            if (length <= 0) {
                _length = 0;
                return false;
            }
            _length = int(length);
        } else {
            _length = int(std::fread(_buffer, 1, buffer_size, _stream));
        }
        return _length != 0;
    }

    template <class T>
    bool read_integer_from_stream(T& value) {
        if (!skip_spaces()) return false;
        int c = read_char_raw();

        bool negative = false;
        if (c == '-') {
            negative = true;
            c = read_char_raw();
        }

        if constexpr (internal::is_signed_v<T>) {
            T result = 0;
            while ('0' <= c && c <= '9') {
                result = negative ? result * 10 - (c - '0')
                                  : result * 10 + (c - '0');
                c = read_char_raw();
            }
            value = result;
        } else {
            T result = 0;
            while ('0' <= c && c <= '9') {
                result = result * 10 + T(c - '0');
                c = read_char_raw();
            }
            value = negative ? T(0) - result : result;
        }
        return true;
    }

    bool prepare_number() {
        if (_length - _position >= 64) return true;
        const int remaining = _length - _position;
        if (remaining > 0) std::memmove(_buffer, _buffer + _position, remaining);
        const int added = int(std::fread(_buffer + remaining, 1, buffer_size - remaining, _stream));
        _position = 0;
        _length = remaining + added;
        if (_length < buffer_size) _buffer[_length] = '\0';
        return _length != 0;
    }

   public:
    explicit FastInput(std::FILE* stream = stdin)
        : _stream(stream),
          _position(0),
          _length(0),
          _file_descriptor(::fileno(stream)),
          _streaming([&] {
              struct stat status;
              return _file_descriptor >= 0
                     && ::fstat(_file_descriptor, &status) == 0
                     && !S_ISREG(status.st_mode);
          }()) {}

    FastInput(const FastInput&) = delete;
    FastInput& operator=(const FastInput&) = delete;

    int read_char_raw() {
        if (_position == _length && !refill()) return EOF;
        return _buffer[_position++];
    }

    bool skip_spaces() {
        int c = read_char_raw();
        while (c != EOF && c <= ' ') c = read_char_raw();
        if (c == EOF) return false;
        --_position;
        return true;
    }

    bool read(char& value) {
        if (!skip_spaces()) return false;
        value = char(read_char_raw());
        return true;
    }

    bool read(std::string& value) {
        if (!skip_spaces()) return false;
        value.clear();
        while (true) {
            const int begin = _position;
            while (_position < _length &&
                   static_cast<unsigned char>(_buffer[_position]) > ' ') {
                ++_position;
            }
            value.append(_buffer + begin, _position - begin);
            if (_position < _length) {
                ++_position;
                return true;
            }
            if (!refill()) return true;
        }
    }

    bool read(bool& value) {
        int x;
        if (!read(x)) return false;
        value = x != 0;
        return true;
    }

    template <class T>
    std::enable_if_t<
        internal::is_integral_v<T>
            && !std::is_same_v<std::remove_cv_t<T>, bool>
            && !std::is_same_v<std::remove_cv_t<T>, char>,
        bool
    >
    read(T& value) {
        if (_streaming) return read_integer_from_stream(value);
        if (!prepare_number()) return false;
        int c = static_cast<unsigned char>(_buffer[_position++]);
        while (c <= ' ') c = static_cast<unsigned char>(_buffer[_position++]);

        bool negative = false;
        if (c == '-') {
            negative = true;
            c = static_cast<unsigned char>(_buffer[_position++]);
        }

        if constexpr (internal::is_signed_v<T>) {
            T result = 0;
            while ('0' <= c && c <= '9') {
                const int first = c - '0';
                const int second = static_cast<unsigned char>(_buffer[_position]) - '0';
                if (0 <= second && second <= 9) {
                    result = negative ? result * 100 - (first * 10 + second)
                                      : result * 100 + (first * 10 + second);
                    ++_position;
                } else {
                    result = negative ? result * 10 - first : result * 10 + first;
                }
                c = static_cast<unsigned char>(_buffer[_position++]);
            }
            value = result;
        } else {
            T result = 0;
            while ('0' <= c && c <= '9') {
                const unsigned first = unsigned(c - '0');
                const int second = static_cast<unsigned char>(_buffer[_position]) - '0';
                if (0 <= second && second <= 9) {
                    result = result * 100 + T(first * 10 + unsigned(second));
                    ++_position;
                } else {
                    result = result * 10 + T(first);
                }
                c = static_cast<unsigned char>(_buffer[_position++]);
            }
            value = negative ? T(0) - result : result;
        }
        if (_position > _length) _position = _length;
        return true;
    }

    template <class T>
    std::enable_if_t<std::is_floating_point_v<T>, bool>
    read(T& value) {
        if (!skip_spaces()) return false;
        int c = read_char_raw();
        bool negative = false;
        if (c == '-' || c == '+') {
            negative = c == '-';
            c = read_char_raw();
        }

        long double result = 0;
        while ('0' <= c && c <= '9') {
            result = result * 10 + (c - '0');
            c = read_char_raw();
        }
        if (c == '.') {
            long double place = 0.1L;
            c = read_char_raw();
            while ('0' <= c && c <= '9') {
                result += (c - '0') * place;
                place *= 0.1L;
                c = read_char_raw();
            }
        }
        if (c == 'e' || c == 'E') {
            c = read_char_raw();
            bool exponent_negative = false;
            if (c == '-' || c == '+') {
                exponent_negative = c == '-';
                c = read_char_raw();
            }
            int exponent = 0;
            while ('0' <= c && c <= '9') {
                exponent = exponent * 10 + (c - '0');
                c = read_char_raw();
            }
            long double scale = 1;
            long double power = 10;
            while (exponent > 0) {
                if (exponent & 1) scale *= power;
                power *= power;
                exponent >>= 1;
            }
            result = exponent_negative ? result / scale : result * scale;
        }
        value = static_cast<T>(negative ? -result : result);
        return true;
    }

    template <class T>
    std::enable_if_t<
        internal::has_val_method_v<T>
            && !internal::is_integral_v<T>
            && !internal::is_range_v<T>,
        bool
    >
    read(T& value) {
        long long x;
        if (!read(x)) return false;
        if constexpr (internal::has_static_mod_raw_v<T>) {
            if (x >= 0 && uint64_t(x) < uint64_t(T::mod())) {
                value = T::raw(uint32_t(x));
            } else {
                value = T(x);
            }
        } else {
            value = T(x);
        }
        return true;
    }

    template <class First, class Second>
    bool read(std::pair<First, Second>& value) {
        if (!read(value.first)) return false;
        return read(value.second);
    }

    template <class Range>
    std::enable_if_t<
        internal::is_range_v<Range>
            && !internal::is_string_like_v<Range>,
        bool
    >
    read(Range& range) {
        using StoredValue = internal::range_stored_value_t<Range>;
        constexpr bool nested = internal::is_range_v<StoredValue>
                                && !internal::is_string_like_v<StoredValue>;

        for (auto&& value : range) {
            if constexpr (std::is_same_v<StoredValue, bool> && !nested) {
                bool x;
                if (!read(x)) return false;
                value = x;
            } else {
                if (!read(value)) return false;
            }
        }
        return true;
    }

    template <class First, class Second, class... Rest>
    bool read(First& first, Second& second, Rest&... rest) {
        if (!read(first)) return false;
        return read(second, rest...);
    }

    template <class T>
    FastInput& operator>>(T& value) {
        if (!read(value)) std::abort();
        return *this;
    }
};

struct FastOutput {
    static constexpr int buffer_size = 1 << 20;

   private:
    inline static const auto digit_quads = [] {
        std::array<char, 40000> result{};
        for (int i = 0; i < 10000; i++) {
            int value = i;
            for (int j = 3; j >= 0; j--) {
                result[4 * i + j] = char('0' + value % 10);
                value /= 10;
            }
        }
        return result;
    }();

    std::FILE* _stream;
    char _buffer[buffer_size];
    int _position;
    int _precision;
    std::chars_format _float_format;
    char _range_separator;
    std::string* _capture = nullptr;

    template <class T>
    std::string format_cell(const T& value) {
        std::string result;
        struct CaptureGuard {
            std::string*& target;
            std::string* previous;
            ~CaptureGuard() { target = previous; }
        } guard{_capture, _capture};
        _capture = &result;
        write(value);
        return result;
    }

    template <class Matrix>
    void write_aligned_matrix(const Matrix& matrix) {
        std::vector<std::vector<std::string>> rows;
        std::vector<std::size_t> widths;
        for (const auto& row : matrix) {
            auto& cells = rows.emplace_back();
            std::size_t column = 0;
            for (const auto& value : row) {
                cells.push_back(format_cell(value));
                if (column == widths.size()) widths.push_back(0);
                widths[column] = std::max(widths[column], cells.back().size());
                ++column;
            }
        }
        bool first = true;
        for (const auto& row : rows) {
            if (!first) write_char('\n');
            first = false;
            for (std::size_t column = 0; column < row.size(); ++column) {
                if (column != 0) write_char(_range_separator);
                for (std::size_t padding = row[column].size();
                     padding < widths[column]; ++padding) {
                    write_char(' ');
                }
                write(row[column]);
            }
        }
    }

   public:
    explicit FastOutput(std::FILE* stream = stdout)
        : _stream(stream),
          _position(0),
          _precision(6),
          _float_format(std::chars_format::general),
          _range_separator(' ') {
        if (_stream == stdout
            && internal::standard_output_instance == nullptr) {
            internal::standard_output_instance = this;
        }
    }

    FastOutput(const FastOutput&) = delete;
    FastOutput& operator=(const FastOutput&) = delete;

    ~FastOutput() {
        flush();
        if (internal::standard_output_instance == this) {
            internal::standard_output_instance = nullptr;
        }
    }

    void flush() {
        if (_position != 0) {
            std::fwrite(_buffer, 1, _position, _stream);
            _position = 0;
        }
        std::fflush(_stream);
    }

    void write_char(char c) {
        if (_capture != nullptr) {
            _capture->push_back(c);
            return;
        }
        if (_position == buffer_size) flush();
        _buffer[_position++] = c;
    }

    void write(const char* s) {
        while (*s != '\0') write_char(*s++);
    }

    void write(const std::string& s) {
        if (_capture != nullptr) {
            _capture->append(s);
            return;
        }
        std::size_t position = 0;
        while (position < s.size()) {
            if (_position == buffer_size) flush();
            const std::size_t copied =
                std::min<std::size_t>(buffer_size - _position, s.size() - position);
            std::memcpy(_buffer + _position, s.data() + position, copied);
            _position += int(copied);
            position += copied;
        }
    }

    void write(char c) {
        write_char(c);
    }

    void write(bool value) {
        write_char(value ? '1' : '0');
    }

    template <class T>
    std::enable_if_t<std::is_floating_point_v<T>>
    write(T value) {
        char digits[128];
        auto [end, error] = std::to_chars(
            digits,
            digits + sizeof(digits),
            value,
            _float_format,
            _precision
        );
        if (error != std::errc()) std::abort();
        for (const char* pointer = digits; pointer != end; pointer++) {
            write_char(*pointer);
        }
    }

    template <class T>
    std::enable_if_t<
        internal::is_integral_v<T>
            && !std::is_same_v<std::remove_cv_t<T>, bool>
            && !std::is_same_v<std::remove_cv_t<T>, char>
    >
    write(T value) {
        using Raw = std::remove_cv_t<T>;
        using Unsigned = internal::make_unsigned_t<Raw>;

        Unsigned magnitude;
        if constexpr (internal::is_signed_v<Raw>) {
            if (value < 0) {
                write_char('-');
                magnitude = Unsigned(0) - Unsigned(value);
            } else {
                magnitude = Unsigned(value);
            }
        } else {
            magnitude = value;
        }

        if (magnitude == 0) {
            write_char('0');
            return;
        }

        unsigned chunks[16];
        int count = 0;
        while (magnitude >= 10000) {
            const Unsigned quotient = magnitude / 10000;
            chunks[count++] = unsigned(magnitude - quotient * 10000);
            magnitude = quotient;
        }
        if (_capture == nullptr && _position > buffer_size - 64) flush();
        char captured[64];
        char* const begin = _capture != nullptr ? captured : _buffer + _position;
        char* destination = begin;
        const unsigned leading = unsigned(magnitude);
        const char* first = digit_quads.data() + 4 * leading;
        int skip = leading < 10 ? 3 : leading < 100 ? 2 : leading < 1000 ? 1 : 0;
        for (; skip < 4; skip++) *destination++ = first[skip];
        while (count--) {
            const char* digits = digit_quads.data() + 4 * chunks[count];
            std::memcpy(destination, digits, 4);
            destination += 4;
        }
        if (_capture != nullptr) {
            _capture->append(begin, destination - begin);
        } else {
            _position += int(destination - begin);
        }
    }

    template <class T>
    std::enable_if_t<
        internal::has_val_method_v<T>
            && !internal::is_integral_v<T>
            && !internal::is_range_v<T>
    >
    write(const T& value) {
        write(value.val());
    }

    template <class First, class Second>
    void write(const std::pair<First, Second>& value) {
        write(value.first);
        write_char(' ');
        write(value.second);
    }

    template <class Range>
    std::enable_if_t<
        internal::is_range_v<Range>
            && !internal::is_string_like_v<Range>
    >
    write(const Range& range) {
        using StoredValue = internal::range_stored_value_t<const Range>;
        constexpr bool nested = internal::is_range_v<StoredValue>
                                && !internal::is_string_like_v<StoredValue>;

        bool first = true;
        for (const auto& value : range) {
            if (!first) write_char(nested ? '\n' : _range_separator);
            first = false;
            if constexpr (std::is_same_v<StoredValue, bool> && !nested) {
                write(static_cast<bool>(value));
            } else {
                write(value);
            }
        }
    }

    template <class First, class... Rest>
    void print(const First& first, const Rest&... rest) {
        write(first);
        ((write_char(' '), write(rest)), ...);
    }

    void println() {
        write_char('\n');
    }

    void set_precision(int precision) {
        _precision = precision;
    }

    void set_fixed(int precision = 6) {
        _float_format = std::chars_format::fixed;
        _precision = precision;
    }

    void set_general(int precision = 6) {
        _float_format = std::chars_format::general;
        _precision = precision;
    }

    void set_range_separator(char separator) {
        _range_separator = separator;
    }

    template <class Matrix>
    void write_aligned(const Matrix& matrix) {
        using Row = internal::range_stored_value_t<const Matrix>;
        using Cell = internal::range_stored_value_t<const Row>;
        static_assert(internal::is_range_v<Row> && !internal::is_string_like_v<Row>,
                      "write_aligned requires a two-dimensional range");
        static_assert(!internal::is_range_v<Cell> || internal::is_string_like_v<Cell>,
                      "write_aligned requires scalar cells");
        write_aligned_matrix(matrix);
    }

    template <class Matrix>
    void println_aligned(const Matrix& matrix) {
        write_aligned(matrix);
        write_char('\n');
    }

    template <class... Args>
    void println(const Args&... args) {
        print(args...);
        write_char('\n');
    }

    template <class T>
    FastOutput& operator<<(const T& value) {
        write(value);
        return *this;
    }
};

}  // namespace utilities
}  // namespace m1une


#line 9 "verify/string/suffix_tree.test.cpp"
#include <map>
#include <set>
#line 14 "verify/string/suffix_tree.test.cpp"

namespace {

void test_features() {
    using Tree = m1une::string::SuffixTree<>;
    Tree empty;
    assert(empty.empty());
    assert(empty.text_length() == 0);
    assert(empty.node_count() == 2);
    assert(empty.count_occurrences(std::string()) == 1);
    assert(empty.distinct_substring_count() == 0);

    std::string text = "ababa";
    Tree tree(text);
    assert(!tree.empty());
    assert(tree.text_length() == 5);
    assert(tree.root() == Tree::root_node);
    assert(tree.node_count() <= 2 * int(text.size()) + 1);
    assert(tree.contains(std::string("bab")));
    assert(!tree.contains(std::string("abb")));
    assert(tree.contains(std::string()));
    assert(tree.count_occurrences(std::string("a")) == 3);
    assert(tree.count_occurrences(std::string("aba")) == 2);
    assert(tree.count_occurrences(std::string("ababa")) == 1);
    assert(tree.count_occurrences(std::string()) == 6);
    assert(tree.distinct_substring_count() == 9);

    auto occurrence = tree.representative_occurrence(std::string("bab"));
    assert(text.substr(occurrence.first, occurrence.second - occurrence.first) == "bab");
    assert((tree.representative_occurrence(std::string("abb")) == std::pair<int, int>(-1, -1)));

    int root_children = 0;
    bool has_terminal_leaf = false;
    tree.for_each_child(tree.root(), [&](int symbol, int child) {
        root_children++;
        assert(tree.node(child).parent == tree.root());
        if (symbol == Tree::terminal_symbol) {
            has_terminal_leaf = true;
            assert(tree.is_leaf(child));
            assert(tree.node(child).suffix_start == int(text.size()));
        }
    });
    assert(root_children == 3);
    assert(has_terminal_leaf);
    assert(tree.child(tree.root(), 'a') != Tree::null_node);
    assert(tree.child_by_index(tree.root(), Tree::terminal_symbol) != Tree::null_node);

    m1une::string::SuffixTree<10, '0'> digits(std::string("012012"));
    assert(digits.contains(std::string("201")));

    tree.clear();
    assert(tree.empty());
    assert(tree.node_count() == 2);
}

void test_randomized() {
    std::uint64_t state = 918273645;
    auto random = [&state]() {
        state ^= state << 7;
        state ^= state >> 9;
        return state;
    };

    for (int trial = 0; trial < 3000; trial++) {
        int n = int(random() % 55);
        std::string text(n, 'a');
        for (char& character : text) character = char('a' + random() % 4);
        m1une::string::SuffixTree<4, 'a'> tree(text);

        assert(tree.node_count() <= std::max(2, 2 * n + 1));
        assert(tree.node(tree.root()).parent == tree.null_node);
        assert(tree.node(tree.root()).suffix_link == tree.root());

        std::map<std::string, int> expected;
        for (int left = 0; left < n; left++) {
            for (int right = left + 1; right <= n; right++) {
                expected[text.substr(left, right - left)]++;
            }
        }
        assert(tree.distinct_substring_count() == int(expected.size()));
        assert(tree.count_occurrences(std::string()) == n + 1);

        for (const auto& entry : expected) {
            const std::string& substring = entry.first;
            int count = entry.second;
            assert(tree.contains(substring));
            assert(tree.count_occurrences(substring) == count);
            auto [left, right] = tree.representative_occurrence(substring);
            assert(0 <= left && left < right && right <= n);
            assert(text.substr(left, right - left) == substring);
        }

        std::set<int> suffix_starts;
        int leaves = 0;
        for (int id = 0; id < tree.node_count(); id++) {
            const auto& node = tree.node(id);
            assert(0 <= node.left && node.left <= node.right && node.right <= n + 1);
            if (id != tree.root()) {
                assert(0 <= node.parent && node.parent < tree.node_count());
                assert(tree.edge_length(id) > 0);
            }
            if (tree.is_leaf(id)) {
                leaves++;
                assert(0 <= node.suffix_start && node.suffix_start <= n);
                suffix_starts.insert(node.suffix_start);
                assert(node.leaf_count == 1);
            } else {
                assert(node.suffix_start == -1);
                assert(node.leaf_count >= (id == tree.root() ? 1 : 2));
            }

            std::vector<std::pair<int, int>> children;
            tree.for_each_child(id, [&](int symbol, int child) {
                children.emplace_back(symbol, child);
            });
            assert(int(children.size()) == node.child_count);

            int position = 0;
            for (int child = node.first_child; child != tree.null_node; child = tree.node(child).next_sibling) {
                assert(position < int(children.size()));
                assert(tree.node(child).parent == id);
                assert(tree.node(child).incoming_symbol == children[position].first);
                assert(child == children[position].second);
                assert(tree.child_by_index(id, children[position].first) == child);
                position++;
            }
            assert(position == int(children.size()));
            for (int i = 1; i < int(children.size()); i++) {
                assert(children[i - 1].first < children[i].first);
            }
        }
        assert(leaves == n + 1);
        assert(int(suffix_starts.size()) == n + 1);
        for (int start = 0; start <= n; start++) assert(suffix_starts.count(start));

        for (int length = 0; length <= n; length++) {
            std::string suffix = text.substr(n - length);
            assert(tree.contains(suffix));
            assert(tree.count_occurrences(suffix) >= 1);
        }

        int query_length = int(random() % 25);
        std::string query(query_length, 'a');
        for (char& character : query) character = char('a' + random() % 4);
        int naive_count = 0;
        for (int position = 0; position + query_length <= n; position++) {
            if (text.compare(position, query_length, query) == 0) naive_count++;
        }
        if (query.empty()) naive_count = n + 1;
        assert(tree.count_occurrences(query) == naive_count);
    }
}

}  // namespace

int main() {
    m1une::utilities::FastInput fast_input;
    m1une::utilities::FastOutput fast_output;

    test_features();
    test_randomized();

    std::string text;
    fast_input >> text;
    m1une::string::SuffixTree<> tree(text);
    fast_output << tree.distinct_substring_count() << '\n';
}
Back to top page