結果

問題 No.263 Common Palindromes Extra
ユーザー maine_honzukimaine_honzuki
提出日時 2021-05-06 09:42:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 317 ms / 2,000 ms
コード長 2,493 bytes
コンパイル時間 2,401 ms
コンパイル使用メモリ 214,348 KB
実行使用メモリ 120,728 KB
最終ジャッジ日時 2023-10-12 02:58:12
合計ジャッジ時間 4,737 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 17 ms
4,488 KB
testcase_04 AC 79 ms
9,760 KB
testcase_05 AC 121 ms
9,288 KB
testcase_06 AC 10 ms
4,348 KB
testcase_07 AC 183 ms
40,060 KB
testcase_08 AC 220 ms
39,596 KB
testcase_09 AC 241 ms
74,000 KB
testcase_10 AC 317 ms
120,728 KB
testcase_11 AC 65 ms
8,964 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//https://ncode.syosetu.com/n4830bu/263/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

struct PalindromicTree {
    PalindromicTree() {
        node* size_m1 = create_node();
        size_m1->suffix_link = 0;
        size_m1->len = -1;
        node* size_0 = create_node();
        size_0->suffix_link = 0;
        size_0->len = 0;
        active_idx = 0;
    }
    struct node {
        map<char, int> link;
        int suffix_link, len, count;
    };
    vector<node> nodes;
    string S;
    int active_idx;
    node* create_node() {
        nodes.emplace_back();
        node* ret = &nodes.back();
        ret->count = 0;
        return ret;
    }
    int find_prev_palindromic_idx(int node_id) {
        const int pos = (int)S.size() - 1;
        while (true) {
            const int opposite_side_idx = pos - 1 - nodes[node_id].len;
            if (opposite_side_idx >= 0 && S[opposite_side_idx] == S.back()) break;
            node_id = nodes[node_id].suffix_link;
        }
        return node_id;
    }
    void add(char ch) {
        S += ch;
        const int prev = find_prev_palindromic_idx(active_idx);
        const auto inserted_result = nodes[prev].link.insert(make_pair(ch, (int)nodes.size()));
        active_idx = inserted_result.first->second;
        if (!inserted_result.second) {
            nodes[active_idx].count++;
            return;
        }
        node* new_node = create_node();
        new_node->count = 1;
        new_node->len = nodes[prev].len + 2;
        if (new_node->len == 1) {
            new_node->suffix_link = 1;
        } else {
            new_node->suffix_link = nodes[find_prev_palindromic_idx(nodes[prev].suffix_link)].link[ch];
        }
    }
    vector<int> build_frequency() {
        vector<int> frequency(nodes.size());
        for (int i = (int)nodes.size() - 1; i > 0; i--) {
            frequency[i] += nodes[i].count;
            frequency[nodes[i].suffix_link] += frequency[i];
        }
        return frequency;
    }
};

int main() {
    string S, T;
    cin >> S >> T;
    string ST = S + "#$" + T;
    auto maine = [&](string S) {
        PalindromicTree book;
        for (auto&& c : S) {
            book.add(c);
        }
        auto frequency = book.build_frequency();
        ll ret = 0;
        for (int i = 2; i < frequency.size(); i++) {
            ret += 1ll * frequency[i] * (frequency[i] - 1) / 2;
        }
        return ret;
    };
    cout << maine(ST) - maine(S) - maine(T) << endl;
}
0