競技プログラミング用の C++ ライブラリ
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A"
// PalindromicTree を、すべての部分文字列を調べる愚直な実装とランダムな短い文字列で比べる
// (異なる回文の個数、各回文の出現回数・長さ・最初の終わりの位置、各位置で終わる最長の回文、親、suffix link、子)
// 問題の入力は使わず、すべて一致したら Hello World を出力する
#include "../../../src/string/palindromic-tree.hpp"
#include <algorithm>
#include <iostream>
#include <map>
#include <random>
#include <string>
#include <vector>
bool ok = true;
void check(bool cond, const char* what) {
if (!cond && ok) std::cerr << "failed: " << what << std::endl;
if (!cond) ok = false;
}
bool is_palindrome(const std::string& t) { return std::equal(t.begin(), t.end(), t.rbegin()); }
// 頂点 v の回文を、最初に現れた位置から取り出す
template <typename PT>
std::string str(const PT& pt, const std::string& s, long long v) {
return s.substr(pt.first_end(v) - pt.len(v) + 1, pt.len(v));
}
int main() {
std::mt19937_64 rng(314);
for (int rep = 0; rep < 3000 && ok; rep++) {
int n = rng() % 20 + 1, sigma = rng() % 3 + 1;
std::string s(n, 'a');
for (auto& c : s) c = 'a' + rng() % sigma;
PalindromicTree<26, 'a'> pt;
// 1 文字ずつ追加し、そのたびに最長の回文接尾辞を確かめる
for (int i = 0; i < n; i++) {
long long v = pt.add(s[i]);
int best = 0;
for (int l = 0; l <= i; l++) {
if (is_palindrome(s.substr(l, i - l + 1))) {
best = i - l + 1;
break;
}
}
check(v == pt.longest_suffix(i) && pt.len(v) == best && str(pt, s, v) == s.substr(i - best + 1, best), "add / longest_suffix");
}
// 愚直: 回文ごとの出現回数と、最初に現れたときの終わりの位置
std::map<std::string, int> cnt, first;
for (int r = 0; r < n; r++) {
for (int l = 0; l <= r; l++) {
std::string t = s.substr(l, r - l + 1);
if (!is_palindrome(t)) continue;
if (!cnt.count(t)) first[t] = r;
cnt[t]++;
}
}
check(pt.count_distinct() == static_cast<long long>(cnt.size()) && pt.size() == pt.count_distinct() + 2, "count_distinct");
check(pt.length() == n, "length");
auto occ = pt.occurrences();
std::vector<std::string> strs(pt.size());
for (long long v = 2; v < pt.size(); v++) {
std::string t = str(pt, s, v);
strs[v] = t;
check(cnt.count(t) && occ[v] == cnt[t] && first[t] == pt.first_end(v), "occurrences / first_end");
// 頂点の番号は最初に現れた終わりの位置の順
if (v > 2) check(pt.first_end(v - 1) <= pt.first_end(v), "頂点の番号の順");
}
check(occ[0] == 0 && occ[1] == 0 && pt.len(0) == -1 && pt.len(1) == 0, "根");
for (long long v = 2; v < pt.size(); v++) {
const std::string& t = strs[v];
// 親は両端を除いた回文
long long p = pt.parent(v);
if (t.size() == 1) {
check(p == 0, "parent (長さ 1)");
} else if (t.size() == 2) {
check(p == 1, "parent (長さ 2)");
} else {
check(strs[p] == t.substr(1, t.size() - 2), "parent");
}
check(pt.child(p, t[0]) == v, "child");
// suffix link は自分より短い最長の空でない回文接尾辞
long long link = 1;
for (size_t l = 1; l < t.size(); l++) {
if (is_palindrome(t.substr(l))) {
link = -1;
for (long long u = 2; u < pt.size(); u++)
if (strs[u] == t.substr(l)) link = u;
break;
}
}
check(pt.suffix_link(v) == link, "suffix_link");
}
// docs の例: suffix link の木での深さから、回文の部分文字列の個数 (重複を含む) を求める
std::vector<long long> depth(pt.size(), 0);
for (long long v = 2; v < pt.size(); v++) depth[v] = depth[pt.suffix_link(v)] + 1;
long long total = 0, expect = 0;
for (long long i = 0; i < pt.length(); i++) total += depth[pt.longest_suffix(i)];
for (const auto& [t, c] : cnt) expect += c;
check(total == expect, "回文の部分文字列の個数");
// 存在しない子は -1
check(pt.child(1, 'z') == -1 || s.find("zz") != std::string::npos, "child なし");
}
// 整数の列から作る
{
PalindromicTree<3, 0> pt(std::vector<int>{0, 1, 2, 1, 0});
check(pt.count_distinct() == 5 && pt.len(pt.longest_suffix(4)) == 5, "整数の列");
}
// 長い列でも速く動く (1 文字だけの列は回文が長い)
{
PalindromicTree<26, 'a'> pt(std::string(1000000, 'a'));
check(pt.count_distinct() == 1000000 && pt.occurrences()[2] == 1000000, "長い列");
}
if (ok) std::cout << "Hello World" << std::endl;
}#line 1 "test/unittest/string/palindromic-tree.test.cpp"
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A"
// PalindromicTree を、すべての部分文字列を調べる愚直な実装とランダムな短い文字列で比べる
// (異なる回文の個数、各回文の出現回数・長さ・最初の終わりの位置、各位置で終わる最長の回文、親、suffix link、子)
// 問題の入力は使わず、すべて一致したら Hello World を出力する
#line 2 "src/string/palindromic-tree.hpp"
/**
* @file palindromic-tree.hpp
* @brief 回文木 (Eertree)
*/
#include <array>
#include <cassert>
#include <vector>
/**
* @brief 回文木 (Eertree, Palindromic Tree)
*
* 列の空でない回文の部分列 (種類) をすべて頂点として持つ。末尾に 1 文字ずつ追加でき、全体で O(n) (文字の種類を K として償却 O(nK) のメモリ)
* 頂点 0 は長さ -1 の根 (ODD)、頂点 1 は長さ 0 の根 (EVEN)。回文の頂点は 2 から、初めて現れた順 (最初に現れたときの終わりの位置の順) に番号が付く
* 文字 c は c - Base として 0 以上 K 未満の番号に直す
* 例:
* PalindromicTree<26, 'a'> pt("abacaba");
* pt.count_distinct(); // 7 (a, b, c, aba, aca, bacab, abacaba)
* pt.occurrences()[v]; // 頂点 v の回文の出現回数
* pt.longest_suffix(6); // 位置 6 で終わる最長の回文の頂点 (abacaba)
*
* @tparam K 文字の種類数
* @tparam Base 最小の文字
*/
template <int K = 26, int Base = 'a'>
class PalindromicTree {
struct Node {
std::array<int, K> next;
int len; // 回文の長さ
int link; // suffix link (自分より短い最長の回文接尾辞の頂点)
int parent; // 両端の 1 文字を除いた回文の頂点
int cnt; // この頂点が最長の回文接尾辞になった位置の個数
int first_end; // 初めて現れたときの終わりの位置
};
// メモリを抑えるため、頂点の中身は int で持つ。外へ返すときに long long にする
std::vector<Node> nodes;
std::vector<int> s; // 追加した文字 (番号に直したもの)
std::vector<int> longest; // 各位置で終わる最長の回文の頂点
int last = 1; // 今の列の最長の回文接尾辞の頂点
int add_node(int len, int link, int parent, int first_end) {
Node node;
node.next.fill(-1);
node.len = len, node.link = link, node.parent = parent;
node.cnt = 0, node.first_end = first_end;
nodes.push_back(node);
return nodes.size() - 1;
}
static int index(int c) {
assert(0 <= c - Base && c - Base < K);
return c - Base;
}
// v から suffix link をたどり、位置 i の文字 c を両端に付けられる最初の頂点
int extendable(int v, int i, int c) const {
while (true) {
int j = i - 1 - nodes[v].len;
if (j >= 0 && s[j] == c) return v;
v = nodes[v].link;
}
}
public:
PalindromicTree() {
add_node(-1, 0, -1, -1); // ODD (長さ -1、suffix link は自分自身)
add_node(0, 0, -1, -1); // EVEN (長さ 0、suffix link は ODD)
}
/**
* @brief 列 s の回文木 O(|s|)
* @param s std::string や std::vector<int> など、要素を整数に変換できる列
*/
template <typename R>
explicit PalindromicTree(const R& s) : PalindromicTree() {
for (auto c : s) add(c);
}
/**
* @brief 末尾に文字 c を追加し、追加後の列の最長の回文接尾辞の頂点を返す 償却 O(1) (新しい頂点には O(K))
*/
long long add(int c) {
c = index(c);
const int i = s.size();
s.push_back(c);
int v = extendable(last, i, c);
if (nodes[v].next[c] == -1) {
// 新しい回文 c + (v の回文) + c。suffix link を先に求めてから辺を張る
int len = nodes[v].len + 2;
int link = len == 1 ? 1 : nodes[extendable(nodes[v].link, i, c)].next[c];
int u = add_node(len, link, v, i);
nodes[v].next[c] = u;
}
last = nodes[v].next[c];
nodes[last].cnt++;
longest.push_back(last);
return last;
}
/**
* @brief 頂点の個数 (2 つの根を含む)
*/
long long size() const { return nodes.size(); }
/**
* @brief 異なる空でない回文の個数 (根を除く頂点の個数)
*/
long long count_distinct() const { return nodes.size() - 2; }
/**
* @brief 追加した列の長さ
*/
long long length() const { return s.size(); }
/**
* @brief 頂点 v の回文の長さ (ODD は -1、EVEN は 0)
*/
long long len(long long v) const { return nodes[v].len; }
/**
* @brief 頂点 v の suffix link (自分より短い最長の空でない回文接尾辞の頂点。なければ EVEN = 1)
* 根では、ODD は 0 (自分自身)、EVEN は 0 (ODD)
*/
long long suffix_link(long long v) const { return nodes[v].link; }
/**
* @brief 頂点 v の親 (両端の 1 文字を除いた回文の頂点。長さ 1 なら ODD = 0、長さ 2 なら EVEN = 1、根なら -1)
*/
long long parent(long long v) const { return nodes[v].parent; }
/**
* @brief 頂点 v の回文の両端に文字 c を付けた回文の頂点 (なければ -1)
*/
long long child(long long v, int c) const { return nodes[v].next[index(c)]; }
/**
* @brief 頂点 v の回文が初めて現れたときの終わりの位置 (0-indexed、根は -1)
* 回文は [first_end(v) - len(v) + 1, first_end(v)] に現れる
*/
long long first_end(long long v) const { return nodes[v].first_end; }
/**
* @brief 位置 i (0-indexed) で終わる最長の回文の頂点
*/
long long longest_suffix(long long i) const {
assert(0 <= i && i < static_cast<long long>(longest.size()));
return longest[i];
}
/**
* @brief 各頂点の回文が列に現れる回数 (根は 0) O(size())
* 頂点 v が最長の回文接尾辞になった回数を、suffix link の木で葉の側から足し合わせる
*/
std::vector<long long> occurrences() const {
std::vector<long long> res(nodes.size());
for (int v = 0; v < static_cast<int>(nodes.size()); v++) res[v] = nodes[v].cnt;
// suffix link の先は自分より前に作った頂点なので、番号の大きい順に足せばよい
for (int v = static_cast<int>(nodes.size()) - 1; v >= 2; v--) res[nodes[v].link] += res[v];
res[0] = res[1] = 0;
return res;
}
};
#line 8 "test/unittest/string/palindromic-tree.test.cpp"
#include <algorithm>
#include <iostream>
#include <map>
#include <random>
#include <string>
#line 15 "test/unittest/string/palindromic-tree.test.cpp"
bool ok = true;
void check(bool cond, const char* what) {
if (!cond && ok) std::cerr << "failed: " << what << std::endl;
if (!cond) ok = false;
}
bool is_palindrome(const std::string& t) { return std::equal(t.begin(), t.end(), t.rbegin()); }
// 頂点 v の回文を、最初に現れた位置から取り出す
template <typename PT>
std::string str(const PT& pt, const std::string& s, long long v) {
return s.substr(pt.first_end(v) - pt.len(v) + 1, pt.len(v));
}
int main() {
std::mt19937_64 rng(314);
for (int rep = 0; rep < 3000 && ok; rep++) {
int n = rng() % 20 + 1, sigma = rng() % 3 + 1;
std::string s(n, 'a');
for (auto& c : s) c = 'a' + rng() % sigma;
PalindromicTree<26, 'a'> pt;
// 1 文字ずつ追加し、そのたびに最長の回文接尾辞を確かめる
for (int i = 0; i < n; i++) {
long long v = pt.add(s[i]);
int best = 0;
for (int l = 0; l <= i; l++) {
if (is_palindrome(s.substr(l, i - l + 1))) {
best = i - l + 1;
break;
}
}
check(v == pt.longest_suffix(i) && pt.len(v) == best && str(pt, s, v) == s.substr(i - best + 1, best), "add / longest_suffix");
}
// 愚直: 回文ごとの出現回数と、最初に現れたときの終わりの位置
std::map<std::string, int> cnt, first;
for (int r = 0; r < n; r++) {
for (int l = 0; l <= r; l++) {
std::string t = s.substr(l, r - l + 1);
if (!is_palindrome(t)) continue;
if (!cnt.count(t)) first[t] = r;
cnt[t]++;
}
}
check(pt.count_distinct() == static_cast<long long>(cnt.size()) && pt.size() == pt.count_distinct() + 2, "count_distinct");
check(pt.length() == n, "length");
auto occ = pt.occurrences();
std::vector<std::string> strs(pt.size());
for (long long v = 2; v < pt.size(); v++) {
std::string t = str(pt, s, v);
strs[v] = t;
check(cnt.count(t) && occ[v] == cnt[t] && first[t] == pt.first_end(v), "occurrences / first_end");
// 頂点の番号は最初に現れた終わりの位置の順
if (v > 2) check(pt.first_end(v - 1) <= pt.first_end(v), "頂点の番号の順");
}
check(occ[0] == 0 && occ[1] == 0 && pt.len(0) == -1 && pt.len(1) == 0, "根");
for (long long v = 2; v < pt.size(); v++) {
const std::string& t = strs[v];
// 親は両端を除いた回文
long long p = pt.parent(v);
if (t.size() == 1) {
check(p == 0, "parent (長さ 1)");
} else if (t.size() == 2) {
check(p == 1, "parent (長さ 2)");
} else {
check(strs[p] == t.substr(1, t.size() - 2), "parent");
}
check(pt.child(p, t[0]) == v, "child");
// suffix link は自分より短い最長の空でない回文接尾辞
long long link = 1;
for (size_t l = 1; l < t.size(); l++) {
if (is_palindrome(t.substr(l))) {
link = -1;
for (long long u = 2; u < pt.size(); u++)
if (strs[u] == t.substr(l)) link = u;
break;
}
}
check(pt.suffix_link(v) == link, "suffix_link");
}
// docs の例: suffix link の木での深さから、回文の部分文字列の個数 (重複を含む) を求める
std::vector<long long> depth(pt.size(), 0);
for (long long v = 2; v < pt.size(); v++) depth[v] = depth[pt.suffix_link(v)] + 1;
long long total = 0, expect = 0;
for (long long i = 0; i < pt.length(); i++) total += depth[pt.longest_suffix(i)];
for (const auto& [t, c] : cnt) expect += c;
check(total == expect, "回文の部分文字列の個数");
// 存在しない子は -1
check(pt.child(1, 'z') == -1 || s.find("zz") != std::string::npos, "child なし");
}
// 整数の列から作る
{
PalindromicTree<3, 0> pt(std::vector<int>{0, 1, 2, 1, 0});
check(pt.count_distinct() == 5 && pt.len(pt.longest_suffix(4)) == 5, "整数の列");
}
// 長い列でも速く動く (1 文字だけの列は回文が長い)
{
PalindromicTree<26, 'a'> pt(std::string(1000000, 'a'));
check(pt.count_distinct() == 1000000 && pt.occurrences()[2] == 1000000, "長い列");
}
if (ok) std::cout << "Hello World" << std::endl;
}