競技プログラミング用 C++ ライブラリ
test/data_dependent_tree.test.cpp · GitHub で見る
#define PROBLEM "https://judge.yosupo.jp/problem/zalgorithm"
#include "string/data_dependent_tree.hpp"
#include "components/fast_io.hpp"
int main() {
FastInput input;
FastOutput output;
std::string s;
input.read(s);
DataDependentTree tree;
int root = tree.build(s);
for (std::size_t i = 0; i < s.size(); ++i) {
if (i) output.write(' ');
output.write(tree.lcp(root, 0, root, i));
}
output.write('\n');
}
#line 1 "test/data_dependent_tree.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/zalgorithm"
#line 1 "string/data_dependent_tree.hpp"
#line 1 "components/splitmix64.hpp"
#include <cstdint>
inline std::uint64_t splitmix64(std::uint64_t x) {
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9ULL;
x = (x ^ (x >> 27)) * 0x94d049bb133111ebULL;
return x ^ (x >> 31);
}
class SplitMix64 {
std::uint64_t state;
public:
explicit SplitMix64(std::uint64_t seed = 0) : state(seed) {}
std::uint64_t operator()() { return splitmix64(state += 0x9e3779b97f4a7c15ULL); }
};
#line 5 "string/data_dependent_tree.hpp"
#include <algorithm>
#include <cassert>
#include <chrono>
#include <cstddef>
#line 10 "string/data_dependent_tree.hpp"
#include <limits>
#include <stdexcept>
#include <string>
#include <string_view>
#include <unordered_map>
#include <utility>
#include <vector>
class DataDependentTree {
using Size = std::uint64_t;
struct Node {
Size length = 0, count = 0, priority = 0;
int level = -1, child = 0;
std::vector<int> children;
std::vector<Size> prefix;
};
struct Part { int node; Size count; };
using Forest = std::vector<Part>;
std::vector<Node> nodes_;
std::unordered_multimap<Size, int> signatures_;
Size seed_;
static Size random_seed() {
auto now = std::chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(Size(now) ^ Size(reinterpret_cast<std::uintptr_t>(&now)));
}
void check([[maybe_unused]] int v) const { assert(0 <= v && std::size_t(v) < nodes_.size()); }
static void push(Forest &out, int v, Size count = 1) {
if (!count) return;
if (!out.empty() && out.back().node == v) out.back().count += count;
else out.push_back({v, count});
}
static int pop(Forest &out) {
int v = out.back().node;
if (!--out.back().count) out.pop_back();
return v;
}
int intern(int level, int child, Size count, std::vector<int> children = {}) {
Size hash = splitmix64(seed_ + Size(level));
hash = splitmix64(hash ^ count);
hash = splitmix64(hash ^ unsigned(child));
for (int v : children) hash = splitmix64(hash ^ unsigned(v));
auto [first, last] = signatures_.equal_range(hash);
for (; first != last; ++first) {
const auto &x = nodes_[first->second];
if (x.level == level && x.child == child && x.count == count && x.children == children) return first->second;
}
if (nodes_.size() >= std::size_t(std::numeric_limits<int>::max())) throw std::length_error("DataDependentTree nodes");
int id = int(nodes_.size());
Node x;
x.level = level, x.child = child, x.count = count, x.priority = splitmix64(seed_ + unsigned(id));
if (child) {
assert(count && nodes_[child].length <= std::numeric_limits<Size>::max() / count);
x.length = nodes_[child].length * count;
} else {
x.children = std::move(children);
x.prefix.push_back(0);
for (int v : x.children) {
assert(nodes_[v].length <= std::numeric_limits<Size>::max() - x.length);
x.length += nodes_[v].length;
x.prefix.push_back(x.length);
}
}
nodes_.push_back(std::move(x));
try { signatures_.emplace(hash, id); }
catch (...) { nodes_.pop_back(); throw; }
return id;
}
int run(int v, Size count, int level) { return intern(level, v, count); }
int sequence(std::vector<int> children, int level) {
Size count = children.size();
return intern(level, 0, count, std::move(children));
}
int trim(int v) const {
while (nodes_[v].level > 0 && nodes_[v].count == 1)
v = nodes_[v].child ? nodes_[v].child : nodes_[v].children[0];
return v;
}
void children(int v, Forest &out, bool reverse) const {
const auto &x = nodes_[v];
assert(x.level > 0);
if (x.child) push(out, x.child, x.count);
else if (reverse) for (auto it = x.children.rbegin(); it != x.children.rend(); ++it) push(out, *it);
else for (int c : x.children) push(out, c);
}
void expand(Forest &out, bool reverse) const {
int v = pop(out);
children(v, out, reverse);
}
Forest boundary(Forest &side, int level, bool left) const {
Forest result;
while (!side.empty() && nodes_[side.back().node].level == level) {
push(result, side.back().node, side.back().count);
side.pop_back();
}
if (!side.empty()) {
while (nodes_[side.back().node].level > level + 1) expand(side, !left);
int v = pop(side);
assert(nodes_[v].level == level + 1);
children(v, result, left);
}
if (left) std::reverse(result.begin(), result.end());
return result;
}
Forest group(const Forest &input, int level) {
Forest result;
if (level & 1) {
for (auto part : input) push(result, run(part.node, part.count, level));
} else {
std::vector<int> current;
auto flush = [&] {
if (!current.empty()) {
push(result, sequence(std::move(current), level));
current.clear();
}
};
for (auto [v, count] : input) {
if (!current.empty() && nodes_[current.back()].priority >= nodes_[v].priority) flush();
current.push_back(v);
if (count > 1) {
flush();
if (count > 2) push(result, sequence(std::vector<int>{v}, level), count - 2);
current.push_back(v);
}
}
flush();
}
return result;
}
int repair(Forest left, Forest right = {}) {
Forest middle;
for (int level = 0;; ++level) {
if (middle.empty() && left.empty() && right.empty()) return 0;
if (middle.empty() && left.empty() && right.size() == 1 && right[0].count == 1) return trim(right[0].node);
if (middle.empty() && right.empty() && left.size() == 1 && left[0].count == 1) return trim(left[0].node);
auto before = boundary(left, level, true), after = boundary(right, level, false);
for (auto part : middle) push(before, part.node, part.count);
for (auto part : after) push(before, part.node, part.count);
if (left.empty() && right.empty() && before.size() == 1 && before[0].count == 1) return trim(before[0].node);
middle = group(before, level + 1);
}
}
void check_sum(int a, int b) const {
if (nodes_[b].length > std::numeric_limits<Size>::max() - nodes_[a].length) throw std::length_error("DataDependentTree length");
}
Forest cursor(int root, Size p = 0) const {
Forest out;
if (p == nodes_[root].length) return out;
out.reserve(std::size_t(nodes_[root].level) + 8);
while (p) {
const auto &x = nodes_[root];
if (x.child) {
Size width = nodes_[x.child].length, count = p / width;
p %= width;
push(out, x.child, x.count - count - bool(p));
if (!p) return out;
root = x.child;
} else {
std::size_t j = std::upper_bound(x.prefix.begin(), x.prefix.end(), p) - x.prefix.begin() - 1;
for (std::size_t i = x.children.size(); i-- > j + 1;) push(out, x.children[i]);
p -= x.prefix[j]; root = x.children[j];
}
}
push(out, root);
return out;
}
Size common_prefix(Forest left, Forest right, Size limit) const {
Size matched = 0;
while (matched < limit) {
auto x = left.back(), y = right.back();
if (x.node == y.node) {
Size count = std::min(x.count, y.count), length = nodes_[x.node].length * count;
if (length >= limit - matched) return limit;
matched += length;
if (!(left.back().count -= count)) left.pop_back();
if (!(right.back().count -= count)) right.pop_back();
} else {
int hl = nodes_[x.node].level, hr = nodes_[y.node].level;
if (!hl && !hr) return matched;
if (hl >= hr) expand(left, true);
if (hr >= hl) expand(right, true);
}
}
return matched;
}
public:
using size_type = Size;
explicit DataDependentTree(Size seed = random_seed()) : nodes_(257), seed_(seed) {
for (int v = 1; v <= 256; ++v) {
nodes_[v].length = nodes_[v].count = 1;
nodes_[v].level = 0;
nodes_[v].priority = splitmix64(seed_ + unsigned(v));
}
}
DataDependentTree(const DataDependentTree &) = default;
DataDependentTree &operator=(const DataDependentTree &other) {
if (this != &other) { DataDependentTree next(other); swap(next); }
return *this;
}
DataDependentTree(DataDependentTree &&other) : DataDependentTree(other.seed_) { swap(other); }
DataDependentTree &operator=(DataDependentTree &&other) {
if (this != &other) { DataDependentTree next(std::move(other)); swap(next); }
return *this;
}
void swap(DataDependentTree &other) noexcept {
nodes_.swap(other.nodes_); signatures_.swap(other.signatures_); std::swap(seed_, other.seed_);
}
Size size(int root) const { check(root); return nodes_[root].length; }
bool empty(int root) const { check(root); return !root; }
int height(int root) const { check(root); return std::max(0, nodes_[root].level); }
std::size_t storage_nodes() const { return nodes_.size() - 1; }
void reserve(std::size_t count) {
if (count >= std::size_t(std::numeric_limits<int>::max())) throw std::length_error("DataDependentTree nodes");
nodes_.reserve(std::max(count + 1, std::size_t(257)));
signatures_.reserve(count);
}
int build(std::string_view text) {
Forest current;
current.reserve(text.size());
for (unsigned char c : text) push(current, int(c) + 1);
for (int level = 1; !current.empty(); ++level) {
if (current.size() == 1 && current[0].count == 1) return trim(current[0].node);
current = group(current, level);
}
return 0;
}
int concat(int a, int b) {
check(a); check(b); check_sum(a, b);
if (!a || !b) return a ? a : b;
return repair(Forest{{a, 1}}, Forest{{b, 1}});
}
std::pair<int, int> split(int root, Size p) {
check(root); assert(p <= size(root));
if (!p) return {0, root};
if (p == size(root)) return {root, 0};
Forest left, right;
int v = root;
while (nodes_[v].level > 0) {
const auto &x = nodes_[v];
if (x.child) {
Size width = nodes_[x.child].length, count = p / width;
push(left, x.child, count);
p %= width;
push(right, x.child, x.count - count - bool(p));
if (!p) break;
v = x.child;
} else {
std::size_t j = std::upper_bound(x.prefix.begin(), x.prefix.end(), p) - x.prefix.begin() - 1;
for (std::size_t i = 0; i < j; ++i) push(left, x.children[i]);
p -= x.prefix[j];
for (std::size_t i = x.children.size(); i-- > j + 1;) push(right, x.children[i]);
v = x.children[j];
if (!p) { push(right, v); break; }
}
}
int a = repair(std::move(left)), b = repair({}, std::move(right));
return {a, b};
}
int substr(int root, Size l, Size r) {
assert(l <= r && r <= size(root));
if (l == r) return 0;
int prefix = split(root, r).first;
return split(prefix, l).second;
}
int insert(int root, Size p, int text) {
check(root); check(text); assert(p <= size(root)); check_sum(root, text);
if (!text) return root;
auto [a, b] = split(root, p);
return concat(concat(a, text), b);
}
int insert(int root, Size p, std::string_view text) { return insert(root, p, build(text)); }
int erase(int root, Size l, Size r) {
assert(l <= r && r <= size(root));
if (l == r) return root;
auto [a, rest] = split(root, l);
int b = split(rest, r - l).second;
return concat(a, b);
}
int set(int root, Size p, char c) {
assert(p < size(root));
auto [a, rest] = split(root, p);
int b = split(rest, 1).second;
return concat(concat(a, int(static_cast<unsigned char>(c)) + 1), b);
}
int repeat(int root, Size count) {
check(root);
if (count && size(root) > std::numeric_limits<Size>::max() / count) throw std::length_error("DataDependentTree length");
int result = 0;
while (count) {
if (count & 1) result = concat(result, root);
count >>= 1;
if (count) root = concat(root, root);
}
return result;
}
char at(int root, Size p) const {
assert(p < size(root));
while (nodes_[root].level > 0) {
const auto &x = nodes_[root];
if (x.child) { root = x.child; p %= nodes_[root].length; }
else {
std::size_t j = std::upper_bound(x.prefix.begin(), x.prefix.end(), p) - x.prefix.begin() - 1;
p -= x.prefix[j]; root = x.children[j];
}
}
return static_cast<char>(root - 1);
}
bool equal(int a, int b) const { check(a); check(b); return a == b; }
Size lcp(int a, int b, Size limit = std::numeric_limits<Size>::max()) const {
check(a); check(b);
limit = std::min({limit, size(a), size(b)});
if (!limit || a == b) return limit;
return common_prefix(cursor(a), cursor(b), limit);
}
Size lcp(int a, Size p, int b, Size q, Size limit = std::numeric_limits<Size>::max()) const {
check(a); check(b); assert(p <= size(a) && q <= size(b));
limit = std::min({limit, size(a) - p, size(b) - q});
if (!limit || (a == b && p == q)) return limit;
return common_prefix(cursor(a, p), cursor(b, q), limit);
}
int compare(int a, int b) const {
Size common = lcp(a, b);
if (common == std::min(size(a), size(b))) return (size(a) > size(b)) - (size(a) < size(b));
unsigned char x = at(a, common), y = at(b, common);
return (x > y) - (x < y);
}
std::string str(int root) const {
check(root);
std::string result;
if (size(root) > result.max_size()) throw std::length_error("DataDependentTree string");
result.reserve(std::size_t(size(root)));
auto pending = cursor(root);
while (!pending.empty()) {
int v = pending.back().node;
if (!nodes_[v].level) {
result.append(std::size_t(pending.back().count), static_cast<char>(v - 1));
pending.pop_back();
} else expand(pending, true);
}
return result;
}
};
#line 1 "components/fast_io.hpp"
#line 5 "components/fast_io.hpp"
#include <array>
#include <bit>
#line 8 "components/fast_io.hpp"
#include <charconv>
#line 11 "components/fast_io.hpp"
#include <cstdio>
#include <cstring>
#include <memory>
#line 17 "components/fast_io.hpp"
#include <type_traits>
#if defined(__unix__) || defined(__APPLE__)
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#endif
namespace fast_io_detail { struct BulkInput; struct BulkOutput; }
class FastInput {
friend struct fast_io_detail::BulkInput;
static constexpr std::size_t capacity = 1 << 16;
FILE *file = nullptr;
std::unique_ptr<char[]> buffer;
const char *current = "", *end = current;
std::string scratch;
void *mapping = nullptr;
std::size_t mapped_size = 0;
bool refill() {
if (!file) return false;
std::size_t n = std::fread(buffer.get(), 1, capacity, file);
current = buffer.get();
end = current + n;
return n;
}
static const char *scan(const char *p, const char *end) {
while (end - p >= 8) {
std::uint64_t x;
std::memcpy(&x, p, 8);
if ((x - 0x2121212121212121ULL) & ~x & 0x8080808080808080ULL) break;
p += 8;
}
while (p != end && static_cast<unsigned char>(*p) > ' ') ++p;
return p;
}
public:
explicit FastInput(FILE *input = stdin) {
#if defined(__unix__) || defined(__APPLE__)
struct stat status;
int fd = fileno(input);
auto offset = ftello(input);
if (fd >= 0 && offset >= 0 && !fstat(fd, &status) && S_ISREG(status.st_mode) && status.st_size > offset) {
mapped_size = status.st_size;
mapping = mmap(nullptr, mapped_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (mapping != MAP_FAILED) {
current = static_cast<const char *>(mapping) + offset;
end = static_cast<const char *>(mapping) + mapped_size;
return;
}
mapping = nullptr;
}
#endif
file = input;
buffer = std::make_unique_for_overwrite<char[]>(capacity);
}
explicit FastInput(std::string_view input) : current(input.empty() ? "" : input.data()), end(current + input.size()) {}
FastInput(const FastInput &) = delete;
FastInput &operator=(const FastInput &) = delete;
~FastInput() {
#if defined(__unix__) || defined(__APPLE__)
if (mapping) munmap(mapping, mapped_size);
#endif
}
std::string_view token() {
while (true) {
while (current != end && static_cast<unsigned char>(*current) <= ' ') ++current;
if (current != end) break;
if (!refill()) return {};
}
const char *begin = current;
current = scan(current, end);
if (current != end || !file) return {begin, std::size_t(current - begin)};
scratch.assign(begin, end);
while (refill()) {
current = scan(current, end);
scratch.append(buffer.get(), std::size_t(current - buffer.get()));
if (current != end) break;
}
return scratch;
}
template <class T>
bool read(T &value) {
if constexpr (std::is_integral_v<T> && !std::is_same_v<T, bool>) {
while (true) {
while (current != end && static_cast<unsigned char>(*current) <= ' ') ++current;
if (current != end) break;
if (!refill()) return false;
}
if constexpr (std::is_unsigned_v<T> && sizeof(T) == 4 && std::endian::native == std::endian::little) {
if (end - current >= 11) {
std::uint64_t x;
std::memcpy(&x, current, 8);
if (!(((x + 0x4646464646464646ULL) | (x - 0x3030303030303030ULL)) & 0x8080808080808080ULL)) {
x &= 0x0f0f0f0f0f0f0f0fULL;
x = (x * 10 + (x >> 8)) & 0x00ff00ff00ff00ffULL;
x = (x * 100 + (x >> 16)) & 0x0000ffff0000ffffULL;
std::uint64_t v = std::uint32_t(x * 10000 + (x >> 32));
int n = 8;
for (; n < 10 && static_cast<unsigned char>(current[n]) - unsigned('0') < 10; ++n)
v = v * 10 + (current[n] - '0');
if (static_cast<unsigned char>(current[n]) <= ' ' && v <= 0xffffffffULL) {
current += n;
value = T(v);
return true;
}
}
}
}
T parsed{};
auto result = std::from_chars(current, end, parsed);
if (result.ec == std::errc{} && (result.ptr != end ? static_cast<unsigned char>(*result.ptr) <= ' ' : !file)) {
current = result.ptr;
value = parsed;
return true;
}
}
auto s = token();
if (s.empty()) return false;
if constexpr (std::is_same_v<T, bool>) {
assert(s == "0" || s == "1");
value = s[0] == '1';
} else if constexpr (std::is_integral_v<T>) {
auto result = std::from_chars(s.data(), s.data() + s.size(), value);
assert(result.ec == std::errc{} && result.ptr == s.data() + s.size());
(void)result;
} else value = T(s);
return true;
}
};
class FastOutput {
friend struct fast_io_detail::BulkOutput;
inline static constexpr auto quads = [] {
std::array<std::array<char, 4>, 10000> table{};
for (int i = 0; i < 10000; ++i)
table[i] = {char('0' + i / 1000), char('0' + i / 100 % 10), char('0' + i / 10 % 10), char('0' + i % 10)};
return table;
}();
FILE *file;
std::unique_ptr<char[]> buffer = std::make_unique_for_overwrite<char[]>(1 << 16);
std::size_t capacity = 1 << 16, length = 0;
char *reserve(std::size_t n) {
if (n > capacity - length) {
if (!flush()) throw std::runtime_error("output failed");
if (n > capacity) {
std::size_t next = std::max(n, 2 * capacity);
buffer = std::make_unique_for_overwrite<char[]>(next);
capacity = next;
}
}
return buffer.get() + length;
}
public:
explicit FastOutput(FILE *output = stdout) : file(output) {}
FastOutput(const FastOutput &) = delete;
FastOutput &operator=(const FastOutput &) = delete;
~FastOutput() { flush(); }
bool flush() {
std::size_t offset = 0;
while (offset < length) {
auto n = std::fwrite(buffer.get() + offset, 1, length - offset, file);
if (!n) {
std::memmove(buffer.get(), buffer.get() + offset, length - offset);
length -= offset;
return false;
}
offset += n;
}
length = 0;
return std::fflush(file) == 0;
}
void write(char c) { *reserve(1) = c; ++length; }
void write(__uint128_t value) {
char digits[40];
char *end = digits + sizeof(digits), *begin = end;
do { *--begin = char('0' + value % 10); value /= 10; } while (value);
write(std::string_view(begin, end - begin));
}
void write(__int128_t value) {
__uint128_t magnitude = __uint128_t(value);
if (value < 0) write('-'), magnitude = 0 - magnitude;
write(magnitude);
}
void write(std::string_view s) {
char *out = reserve(s.size());
if (!s.empty()) std::memcpy(out, s.data(), s.size());
length += s.size();
}
template <class T> requires std::is_integral_v<T>
void write(T value) {
char *out = reserve(32);
if constexpr (std::is_same_v<T, bool>) *out = '0' + value, ++length;
else if constexpr (std::is_unsigned_v<T> && sizeof(T) == 4) {
char *p = out;
if (value >= 100000000) {
auto high = value / 100000000, low = value % 100000000;
p = std::to_chars(p, p + 2, high).ptr;
std::memcpy(p, quads[low / 10000].data(), 4);
std::memcpy(p + 4, quads[low % 10000].data(), 4);
p += 8;
} else if (value >= 10000) {
p = std::to_chars(p, p + 4, value / 10000).ptr;
std::memcpy(p, quads[value % 10000].data(), 4);
p += 4;
} else p = std::to_chars(p, p + 4, value).ptr;
length += p - out;
} else length += std::to_chars(out, out + 32, value).ptr - out;
}
template <class T> requires requires(const T &value, char *out) { value.decimal_capacity(); value.to_chars(out); }
void write(const T &value) {
char *out = reserve(value.decimal_capacity());
length += value.to_chars(out) - out;
}
template <class T>
void println(const T &value) { write(value); write('\n'); }
};
#line 4 "test/data_dependent_tree.test.cpp"
int main() {
FastInput input;
FastOutput output;
std::string s;
input.read(s);
DataDependentTree tree;
int root = tree.build(s);
for (std::size_t i = 0; i < s.size(); ++i) {
if (i) output.write(' ');
output.write(tree.lcp(root, 0, root, i));
}
output.write('\n');
}