結果

問題 No.263 Common Palindromes Extra
ユーザー shibh308shibh308
提出日時 2020-07-26 08:34:12
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 760 ms / 2,000 ms
コード長 2,751 bytes
コンパイル時間 2,367 ms
コンパイル使用メモリ 214,648 KB
実行使用メモリ 180,912 KB
最終ジャッジ日時 2023-09-10 09:52:23
合計ジャッジ時間 5,542 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 12 ms
4,380 KB
testcase_04 AC 51 ms
6,912 KB
testcase_05 AC 75 ms
6,360 KB
testcase_06 AC 7 ms
4,376 KB
testcase_07 AC 254 ms
107,548 KB
testcase_08 AC 253 ms
107,188 KB
testcase_09 AC 760 ms
180,912 KB
testcase_10 AC 695 ms
167,836 KB
testcase_11 AC 42 ms
6,112 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

struct EerTree{
    struct Node{
        Node(int len) : len(len), sdep(0), slink(nullptr), cnt(0){}
        int len, sdep;
        map<char, Node*> ch;
        Node* slink;
        int cnt;
    };
    pair<Node*, Node*> root;
    Node* active_point;
    string s;
    EerTree(){
        root.second = new Node(0);
        root.second->slink = root.first = active_point = new Node(-1);
        root.first->slink = root.first;
    }
    EerTree(string inp) : EerTree(){
        for(auto c : inp)
            add(c);
    }
    Node* make(Node* par, char c){
        if(par->ch.find(c) == par->ch.end()){
            par->ch[c] = new Node(par->len + 2);
            Node* sl = par->slink;
            if(par->len == -1)
                sl = root.second;
            else{
                while(1){
                    if(s[s.size() - sl->len - 2] == c){
                        sl = sl->ch[c];
                        break;
                    }else if(sl->len < 0){
                        sl = root.second;
                        break;
                    }else
                        sl = sl->slink;
                }
            }
            par->ch[c]->slink = sl;
            par->ch[c]->sdep = sl->sdep + 1;
        }
        return par->ch[c];
    }
    void add(char c){
        for(s += c; int(s.size()) - active_point->len - 2 < 0 || s[s.size() - active_point->len - 2] != c; active_point = active_point->slink);
        active_point = make(active_point, c);
    }
};

long long search(EerTree::Node* x, EerTree::Node* y){
    long long cnt = 1LL * x->cnt * y->cnt;
    if(x->len <= 0)
        cnt = 0;
    for(auto child : x->ch){
        if(y->ch.find(child.first) != y->ch.end()){
            cnt += search(x->ch[child.first], y->ch[child.first]);
        }
    }
    return cnt;
}

void dump(EerTree::Node* x, std::vector<EerTree::Node*>& v){
    v.emplace_back(x);
    for(auto child : x->ch)
        dump(child.second, v);
}

signed main(){
    string s, t;
    cin >> s >> t;
    EerTree es, et;
    for(auto c : s){
        es.add(c);
        es.active_point->cnt += 1;
    }
    for(auto c : t){
        et.add(c);
        et.active_point->cnt += 1;
    }
    std::vector<EerTree::Node*> sv, tv;
    dump(es.root.first, sv);
    dump(es.root.second, sv);
    dump(et.root.first, tv);
    dump(et.root.second, tv);
    sort(sv.begin(), sv.end(), [](auto x, auto y){return x->sdep > y->sdep;});
    for(auto x : sv)
        x->slink->cnt += x->cnt;
    sort(tv.begin(), tv.end(), [](auto x, auto y){return x->sdep > y->sdep;});
    for(auto x : tv)
        x->slink->cnt += x->cnt;
    cout << search(es.root.first, et.root.first) + search(es.root.second, et.root.second) << endl;

}
0