結果

問題 No.2204 Palindrome Splitting (No Rearrangement ver.)
コンテスト
ユーザー iastm
提出日時 2026-11-18 03:08:11
言語 C++23
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -fplugin=/usr/lib/yukicoder/noavx512-gcc15.so -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 3,249 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,286 ms
コンパイル使用メモリ 175,372 KB
実行使用メモリ 9,868 KB
最終ジャッジ日時 2026-11-18 03:08:16
合計ジャッジ時間 4,273 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 31 WA * 3
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <iostream>
#include <vector>
// #include <kotone/palindrome_tree>
// https://github.com/amiast/cpp-utility

#ifndef KOTONE_PALINDROME_TREE_HPP
#define KOTONE_PALINDROME_TREE_HPP 1

#include <vector>
#include <array>
#include <cassert>

namespace kotone {

// A node in a palindrome tree.
// Includes the following attributes:
// - `size`: the length of the palindrome represented by the node.
// - `link`: the node containing the longest proper palindromic suffix. If `size <= 0`, then `link == 0`.
// - `parent`: the parent node. If `size <= 0`, then `parent == -1`.
// - `children`: the array of children. Uses `-1` for children that do not exist.
//
// Requires `ALPHABET_SIZE >= 0`.
template <int ALPHABET_SIZE> struct palindrome_tree_node {
    static_assert(ALPHABET_SIZE >= 0);
    int size = 0, link = 0, parent = -1;
    std::array<int, ALPHABET_SIZE> children;
    palindrome_tree_node() { children.fill(-1); }
    palindrome_tree_node(int s) : size(s) { children.fill(-1); }
};

// Returns the palindrome tree of the given string.
// Optionally, pass the following functions:
// - `void init(int index, int size)`
// - `void on_push(int pos, int index_longest_suffix)`
//
// Requires `ALPHABET_SIZE >= 0`.
// Requires `0 <= str[i] < ALPHABET_SIZE` for all `i`.
// Reference: https://judge.yosupo.jp/problem/eertree
template <int ALPHABET_SIZE, void (*init)(int, int) = nullptr, void (*on_push)(int, int) = nullptr>
std::vector<palindrome_tree_node<ALPHABET_SIZE>> palindrome_tree(const std::vector<int> &str) {
    int len = str.size();
    for (int i = 0; i < len; i++) assert(0 <= str[i] && str[i] < ALPHABET_SIZE);
    using node = palindrome_tree_node<ALPHABET_SIZE>;
    std::vector<node> tree;
    auto link = [&](int i, int k) {
        while (true) {
            int j = k - 1 - tree[i].size;
            if (j >= 0 && str[j] == str[k]) break;
            i = tree[i].link;
        }
        return i;
    };
    for (int i = 0; i < 2; i++) {
        if constexpr (init) init(i, i - 1);
        tree.emplace_back(i - 1);
    }
    int prev = 1;
    for (int k = 0; k < len; k++) {
        int c = str[k];
        int i = link(prev, k);
        if (tree[i].children[c] == -1) {
            int ni = tree.size();
            if constexpr(init) init(ni, tree[i].size + 2);
            tree.emplace_back(tree[i].size + 2);
            node &n = tree.back();
            n.parent = i;
            int j = link(tree[i].link, k);
            if (int sl = tree[j].children[c]; sl >= 0) n.link = sl;
            else n.link = 1;
            tree[i].children[c] = ni;
        }
        prev = tree[i].children[c];
        if constexpr (on_push) on_push(k, prev);
    }
    return tree;
}

}  // namespace kotone

#endif  // KOTONE_PALINDROME_TRE_HPP

std::vector<int> prev;
void on_push(int, int l) { prev.push_back(l); }

int main() {
    std::string S;
    std::cin >> S;
    int N = S.size();
    std::vector<int> T(N);
    for (int i = 0; i < N; i++) T[i] = S[i] - 'a';
    auto tree = kotone::palindrome_tree<26, nullptr, on_push>(T);
    int result = N;
    for (int r = N - 1; r >= 0;) {
        int s = tree[prev[r]].size;
        result = std::min(result, s);
        r -= s;
    }
    std::cout << result << std::endl;
}
0