cp-library

競技プログラミング用 C++ ライブラリ

View the Project on GitHub qandori/cp-library

:heavy_check_mark: Data Dependent Tree

string/data_dependent_tree.hpp · GitHub で見る

共通ページ

連結・分割・編集できる文字列を管理し、LCP(最長共通接頭辞)と辞書順比較を求めます。同じ管理オブジェクト内では、内容が同じ文字列は構築・編集の履歴によらず同じ木になります。文字列の一致判定に誤判定はありません。

#include "lib/string/data_dependent_tree.hpp"

int main() {
	DataDependentTree tree;
	int a = tree.build("banana");
	int b = tree.set(a, 4, 'd');
	assert(tree.str(b) == "banada");
	assert(tree.str(a) == "banana");
	assert(tree.lcp(a, b) == 4);
	assert(tree.lcp(a, 1, a, 3) == 3);
	assert(tree.lcp(a, 1, a, 3, 2) == 2);
	assert(tree.compare(a, b) > 0);

	auto [left, right] = tree.split(a, 3);
	assert(tree.str(left) == "ban");
	assert(tree.str(right) == "ana");
	assert(tree.equal(tree.concat(left, right), a));
	assert(tree.substr(a, 1, 4) == right);
	int c = tree.insert(a, 3, "!");
	assert(tree.str(c) == "ban!ana");
	assert(tree.erase(c, 3, 4) == a);
	assert(tree.str(tree.repeat(right, 2)) == "anaana");
}

文字列は根の番号 int で扱い、0 は空文字列です。全ての編集は新しい根を返し、既存の根は変更しません。過去の文字列との比較や、異なる版を連結する操作にも対応します。build も既存の文字列を消去しません。

番号は取得した DataDependentTree に属します。異なるオブジェクトで得た番号を混ぜないでください。全体をコピーした場合は、コピー時点までの番号を両方で使えます。コピー後に追加した番号は、その追加先だけで使います。乱数シードを揃えるだけでは番号・木の形の共有にはなりません。

入力は std::string_view で渡し、NUL を含む全 256 種類のバイトに対応します。辞書順は符号なしバイトの順序です。入力を保持する必要はありません。文字数・位置・LCP 長の型は size_type = std::uint64_t、区間は半開区間 [l,r) です。文字数が UINT64_MAX を超える連結・挿入・繰り返しは std::length_error を送出します。位置と区間は文字列の範囲内にしてください。

同一部分の繰り返しをまとめ、内容に応じた乱数の大小で残りをグループ化します。乱択は木の形と実行時間に使い、同一性は子の構造まで照合して判定します。ハッシュ表の衝突による誤判定はありません。既定では実行時にシードを選び、DataDependentTree(seed) で固定できます。計算量は乱択に対する期待値で、最悪時の高さを保証するものではありません。

作成したノードは管理オブジェクトが保持し続けます。使わなくなった根を捨てても、そのノードの領域は解放しません。多数の版を作る場合は、現在の文字数だけでなく編集回数に応じたメモリが必要です。

文字数の最大値を \(n\)、追加する文字列の長さを \(m\)、繰り返し回数を \(k\)、管理しているノード数を \(M\) とします。更新の期待時間・追加領域は償却の評価です。

操作 内容・返り値 計算量
DataDependentTree() / DataDependentTree(seed) 空の文字列群を作成 \(O(1)\)
build(s) 文字列 s の根を返す 期待 \(O(m)\) 時間・追加領域
size(root) / empty(root) / height(root) 文字数 / 空判定 / 木の高さ \(O(1)\)
equal(a,b) 内容の一致判定 \(O(1)\)
lcp(a,b) / lcp(a,b,limit) 先頭からの LCP 長。limit があればその長さまで 期待 \(O(\log n)\)
lcp(a,p,b,q) / lcp(a,p,b,q,limit) a の位置 p と b の位置 q からの LCP 長 期待 \(O(\log n)\)
compare(a,b) 辞書順で小さい / 等しい / 大きいとき -1 / 0 / 1 期待 \(O(\log n)\)
at(root,p) 位置 p のバイトを char で返す 期待 \(O(\log n)\)
concat(a,b) a と b を連結した根 期待 \(O(\log n)\) 時間・追加領域
split(root,p) [0,p) と [p,size(root)) の根の pair<int,int> 期待 \(O(\log n)\) 時間・追加領域
substr(root,l,r) 部分文字列 [l,r) の根 期待 \(O(\log n)\) 時間・追加領域
insert(root,p,other) 位置 p に別の根 other を挿入した根 期待 \(O(\log n)\) 時間・追加領域
insert(root,p,s) 位置 p に文字列 s を挿入した根 期待 \(O(m+\log n)\) 時間・追加領域
erase(root,l,r) / set(root,p,c) 区間の削除 / 1 バイトの置換を行った根 期待 \(O(\log n)\) 時間・追加領域
repeat(root,k) k 回連結した根。k == 0 は空文字列 期待 \(O(\log k\log n)\) 時間・追加領域
str(root) std::string に展開 期待 \(O(n)\) 時間、出力に \(O(n)\) 空間
storage_nodes() 文字ノード 256 個を含む保持ノード数 \(O(1)\)
reserve(count) ノード・同一性照合表の容量を予約 期待 \(O(M+count)\)
swap(other) / コピー / ムーブ構築 管理内容を交換 / 全体を複製 / 全体を移動 交換・ムーブ構築 \(O(1)\)、コピーは保持量に比例

LCP・一致判定・辞書順比較・文字アクセスはノードを追加しません。LCP の開始位置は末尾でもよく、その場合は 0 です。空文字列の高さは 0 です。ムーブ元は空の文字列群として再利用できます。代入は、代入先が保持していた領域の破棄に必要な時間もかかります。

参考: Jean Cardinal・John Iacono, Modular Subset Sum, Dynamic Strings, and Zero-Sum Sets, §4。静的な文字列の Z 配列だけが必要な場合は、線形時間の Z algorithm を使えます。

依存ライブラリ

検証コード

コード

#ifndef DATA_DEPENDENT_TREE_HPP
#define DATA_DEPENDENT_TREE_HPP

#include "../components/splitmix64.hpp"
#include <algorithm>
#include <cassert>
#include <chrono>
#include <cstddef>
#include <cstdint>
#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;
	}
};

#endif
Back to top page