This documentation is automatically generated by competitive-verifier/competitive-verifier
// competitive-verifier: PROBLEM https://judge.yosupo.jp/problem/eertree
#include "../../template/template.hpp"
#include "../../string/palindromic-tree.hpp"
#include "../../other/scanner.hpp"
#include "../../other/printer.hpp"
int main() {
Scanner in(stdin);
Printer out(stdout);
string S;
in.read(S);
PalindromicTree g;
vector< int > vs;
vs.reserve(S.size());
for (auto &c: S) {
vs.emplace_back(g.add(c) - 1);
}
int n = g.size();
vector< int > p(n), s(n);
for (int i = 0; i < n; i++) {
for (auto &e: g[i].link) {
if (e.second >= 0) p[e.second] = i - 1;
}
s[i] = g[i].suffix_link - 1;
}
out.writeln(n - 2);
for (int i = 2; i < n; i++) out.writeln(p[i], s[i]);
out.writeln(vs);
}
#line 1 "test/verify/yosupo-eertree.test.cpp"
// competitive-verifier: PROBLEM https://judge.yosupo.jp/problem/eertree
#line 1 "template/template.hpp"
#include <bits/stdc++.h>
#if __has_include(<atcoder/all>)
#include <atcoder/all>
#endif
using namespace std;
using int64 = long long;
const int64 infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;
struct IoSetup {
IoSetup() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
cerr << fixed << setprecision(10);
}
} iosetup;
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
os << p.first << " " << p.second;
return os;
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
is >> p.first >> p.second;
return is;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
for (int i = 0; i < (int)v.size(); i++) {
os << v[i] << (i + 1 != v.size() ? " " : "");
}
return os;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
for (T &in : v) is >> in;
return is;
}
template <typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) {
return a < b && (a = b, true);
}
template <typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) {
return a > b && (a = b, true);
}
template <typename T = int64>
vector<T> make_v(size_t a) {
return vector<T>(a);
}
template <typename T, typename... Ts>
auto make_v(size_t a, Ts... ts) {
return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
t = v;
}
template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
for (auto &e : t) fill_v(e, v);
}
template <typename F>
struct FixPoint : F {
explicit FixPoint(F &&f) : F(std::forward<F>(f)) {}
template <typename... Args>
decltype(auto) operator()(Args &&...args) const {
return F::operator()(*this, std::forward<Args>(args)...);
}
};
template <typename F>
inline decltype(auto) MFP(F &&f) {
return FixPoint<F>{std::forward<F>(f)};
}
#line 4 "test/verify/yosupo-eertree.test.cpp"
#line 1 "string/palindromic-tree.hpp"
/**
* @brief Palindromic Tree(回文木)
* @see https://math314.hateblo.jp/entry/2016/12/19/005919
*
*/
template <typename T = char>
struct PalindromicTree {
public:
struct Node {
map<T, int> link; // 子のidx
int suffix_link; // 最長回文接尾辞のidx
int len; // 対応する回文の長さ
vector<int> idx; // 対応する回文を最長回文接尾辞とするidx
int delta_link; // 差分が異なる最長回文接尾辞のidx
Node() = default;
Node(int suf, int len) : suffix_link(suf), len(len), delta_link(-1) {}
};
vector<Node> ns;
int ptr;
vector<T> vs;
private:
int find_prev_palindrome(int cur) const {
int pos = (int)vs.size() - 1;
for (;;) {
int rev = pos - 1 - ns[cur].len;
if (rev >= 0 and vs[rev] == vs.back()) break;
cur = ns[cur].suffix_link;
}
return cur;
}
bool output_dfs(int v, int id, vector<T> &ret) const {
if (v == id) return true;
for (auto &nxt : ns[v].link) {
if (output_dfs(nxt.second, id, ret)) {
ret.emplace_back(nxt.first);
return true;
}
}
return false;
}
public:
PalindromicTree() : ptr(0) {
ns.emplace_back(0, -1); // 長さ -1
ns.emplace_back(0, 0); // 長さ 0
}
PalindromicTree(const string &S) : PalindromicTree() { add(S); }
int diff(int t) const {
if (ns[t].suffix_link <= 0) return -1;
return ns[t].len - ns[ns[t].suffix_link].len;
}
int add(const T &x) {
int idx = (int)vs.size();
vs.emplace_back(x);
int cur = find_prev_palindrome(ptr);
auto res = ns[cur].link.insert(make_pair(x, (int)ns.size()));
ptr = res.first->second;
if (res.second) {
ns.emplace_back(-1, ns[cur].len + 2);
if (ns.back().len == 1) {
ns.back().suffix_link = 1;
} else {
ns.back().suffix_link =
ns[find_prev_palindrome(ns[cur].suffix_link)].link[x];
}
if (diff(ptr) == diff(ns.back().suffix_link)) {
ns.back().delta_link = ns[ns.back().suffix_link].delta_link;
} else {
ns.back().delta_link = ns.back().suffix_link;
}
}
ns[ptr].idx.emplace_back(idx);
return ptr;
}
// add(x) のあとに呼び出す
// * init(node_idx, pos): 頂点 node_idx を S[pos,i]
// が回文のときの初期値にする
// * apply(node_idx, pre_idx): 頂点 node_idx に 頂点 pre_idx の結果を加える
// * update: S[i]を末尾とする回文の頂点番号の集合
template <typename I, typename U>
vector<int> update_dp(const I &init, const U &apply) {
int i = (int)vs.size() - 1;
int id = ptr;
vector<int> update;
while (ns[id].len > 0) {
init(id, i + 1 - ns[ns[id].delta_link].len - diff(id));
if (ns[id].suffix_link != ns[id].delta_link) {
apply(id, ns[id].suffix_link);
}
update.emplace_back(id);
id = ns[id].delta_link;
}
return update;
}
void add(const string &s) {
for (auto &x : s) add(x);
}
vector<int> build_frequency() const {
vector<int> ret(ns.size());
for (int i = (int)ns.size() - 1; i > 0; i--) {
ret[i] += (int)ns[i].idx.size();
ret[ns[i].suffix_link] += ret[i];
}
return ret;
}
vector<T> output(int idx) const {
if (idx == 0) return {-1};
if (idx == 1) return {0};
vector<T> ret;
output_dfs(0, idx, ret);
output_dfs(1, idx, ret);
int start = (int)ret.size() - 1;
if (ns[idx].len & 1) --start;
for (int i = start; i >= 0; i--) {
ret.emplace_back(ret[i]);
}
return ret;
}
int size() const { return (int)ns.size(); }
Node &operator[](int idx) { return ns[idx]; }
};
#line 6 "test/verify/yosupo-eertree.test.cpp"
#line 1 "other/scanner.hpp"
/**
* @brief Scanner(高速入力)
*/
struct Scanner {
public:
explicit Scanner(FILE *fp) : fp(fp) {}
template <typename T, typename... E>
void read(T &t, E &...e) {
read_single(t);
read(e...);
}
private:
static constexpr size_t line_size = 1 << 16;
static constexpr size_t int_digits = 20;
char line[line_size + 1] = {};
FILE *fp = nullptr;
char *st = line;
char *ed = line;
void read() {}
static inline bool is_space(char c) { return c <= ' '; }
void reread() {
ptrdiff_t len = ed - st;
memmove(line, st, len);
char *tmp = line + len;
ed = tmp + fread(tmp, 1, line_size - len, fp);
*ed = 0;
st = line;
}
void skip_space() {
while (true) {
if (st == ed) reread();
while (*st && is_space(*st)) ++st;
if (st != ed) return;
}
}
template <typename T, enable_if_t<is_integral<T>::value, int> = 0>
void read_single(T &s) {
skip_space();
if (st + int_digits >= ed) reread();
bool neg = false;
if (is_signed<T>::value && *st == '-') {
neg = true;
++st;
}
typename make_unsigned<T>::type y = *st++ - '0';
while (*st >= '0') {
y = 10 * y + *st++ - '0';
}
s = (neg ? -y : y);
}
template <typename T, enable_if_t<is_same<T, string>::value, int> = 0>
void read_single(T &s) {
s = "";
skip_space();
while (true) {
char *base = st;
while (*st && !is_space(*st)) ++st;
s += string(base, st);
if (st != ed) return;
reread();
}
}
template <typename T>
void read_single(vector<T> &s) {
for (auto &d : s) read(d);
}
};
#line 1 "other/printer.hpp"
/**
* @brief Printer(高速出力)
*/
struct Printer {
public:
explicit Printer(FILE *fp) : fp(fp) {}
~Printer() { flush(); }
template <bool f = false, typename T, typename... E>
void write(const T &t, const E &...e) {
if (f) write_single(' ');
write_single(t);
write<true>(e...);
}
template <typename... T>
void writeln(const T &...t) {
write(t...);
write_single('\n');
}
void flush() {
fwrite(line, 1, st - line, fp);
st = line;
}
private:
FILE *fp = nullptr;
static constexpr size_t line_size = 1 << 16;
static constexpr size_t int_digits = 20;
char line[line_size + 1] = {};
char *st = line;
template <bool f = false>
void write() {}
void write_single(const char &t) {
if (st + 1 >= line + line_size) flush();
*st++ = t;
}
template <typename T, enable_if_t<is_integral<T>::value, int> = 0>
void write_single(T s) {
if (st + int_digits >= line + line_size) flush();
st += to_chars(st, st + int_digits, s).ptr - st;
}
void write_single(const string &s) {
for (auto &c : s) write_single(c);
}
void write_single(const char *s) {
while (*s != 0) write_single(*s++);
}
template <typename T>
void write_single(const vector<T> &s) {
for (size_t i = 0; i < s.size(); i++) {
if (i) write_single(' ');
write_single(s[i]);
}
}
};
#line 9 "test/verify/yosupo-eertree.test.cpp"
int main() {
Scanner in(stdin);
Printer out(stdout);
string S;
in.read(S);
PalindromicTree g;
vector< int > vs;
vs.reserve(S.size());
for (auto &c: S) {
vs.emplace_back(g.add(c) - 1);
}
int n = g.size();
vector< int > p(n), s(n);
for (int i = 0; i < n; i++) {
for (auto &e: g[i].link) {
if (e.second >= 0) p[e.second] = i - 1;
}
s[i] = g[i].suffix_link - 1;
}
out.writeln(n - 2);
for (int i = 2; i < n; i++) out.writeln(p[i], s[i]);
out.writeln(vs);
}
Env | Name | Status | Elapsed | Memory |
---|---|---|---|---|
g++ | binary_00 | AC | 150 ms | 140 MB |
g++ | binary_01 | AC | 145 ms | 140 MB |
g++ | binary_02 | AC | 143 ms | 140 MB |
g++ | binary_03 | AC | 148 ms | 140 MB |
g++ | example_00 | AC | 5 ms | 4 MB |
g++ | example_01 | AC | 5 ms | 4 MB |
g++ | example_02 | AC | 5 ms | 4 MB |
g++ | random_00 | AC | 72 ms | 16 MB |
g++ | random_01 | AC | 72 ms | 17 MB |
g++ | random_small_sigma_00 | AC | 178 ms | 181 MB |
g++ | random_small_sigma_01 | AC | 59 ms | 15 MB |
g++ | random_small_sigma_02 | AC | 61 ms | 15 MB |
g++ | random_small_sigma_03 | AC | 67 ms | 15 MB |
g++ | random_small_sigma_04 | AC | 65 ms | 15 MB |
g++ | short_period_00 | AC | 182 ms | 181 MB |
g++ | short_period_01 | AC | 99 ms | 92 MB |
g++ | short_period_02 | AC | 180 ms | 181 MB |
g++ | short_period_03 | AC | 96 ms | 92 MB |
g++ | short_period_04 | AC | 182 ms | 181 MB |
g++ | short_period_05 | AC | 93 ms | 92 MB |
g++ | short_period_06 | AC | 176 ms | 181 MB |
g++ | short_period_07 | AC | 96 ms | 92 MB |
g++ | short_period_08 | AC | 41 ms | 16 MB |
g++ | short_period_09 | AC | 24 ms | 9 MB |
clang++ | binary_00 | AC | 141 ms | 140 MB |
clang++ | binary_01 | AC | 140 ms | 140 MB |
clang++ | binary_02 | AC | 143 ms | 140 MB |
clang++ | binary_03 | AC | 143 ms | 140 MB |
clang++ | example_00 | AC | 6 ms | 4 MB |
clang++ | example_01 | AC | 5 ms | 4 MB |
clang++ | example_02 | AC | 5 ms | 4 MB |
clang++ | random_00 | AC | 60 ms | 16 MB |
clang++ | random_01 | AC | 60 ms | 17 MB |
clang++ | random_small_sigma_00 | AC | 179 ms | 181 MB |
clang++ | random_small_sigma_01 | AC | 49 ms | 15 MB |
clang++ | random_small_sigma_02 | AC | 51 ms | 15 MB |
clang++ | random_small_sigma_03 | AC | 53 ms | 15 MB |
clang++ | random_small_sigma_04 | AC | 53 ms | 15 MB |
clang++ | short_period_00 | AC | 183 ms | 181 MB |
clang++ | short_period_01 | AC | 97 ms | 92 MB |
clang++ | short_period_02 | AC | 178 ms | 181 MB |
clang++ | short_period_03 | AC | 97 ms | 92 MB |
clang++ | short_period_04 | AC | 184 ms | 181 MB |
clang++ | short_period_05 | AC | 99 ms | 92 MB |
clang++ | short_period_06 | AC | 182 ms | 181 MB |
clang++ | short_period_07 | AC | 94 ms | 92 MB |
clang++ | short_period_08 | AC | 40 ms | 16 MB |
clang++ | short_period_09 | AC | 24 ms | 9 MB |