結果

問題 No.2606 Mirror Relay
ユーザー novaandcabralnovaandcabral
提出日時 2024-04-13 07:50:20
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 94 ms / 2,000 ms
コード長 2,530 bytes
コンパイル時間 1,296 ms
コンパイル使用メモリ 91,256 KB
実行使用メモリ 52,100 KB
最終ジャッジ日時 2024-04-13 07:50:25
合計ジャッジ時間 4,228 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 3 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 2 ms
6,944 KB
testcase_16 AC 2 ms
6,944 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 2 ms
6,944 KB
testcase_19 AC 2 ms
6,944 KB
testcase_20 AC 2 ms
6,944 KB
testcase_21 AC 3 ms
6,944 KB
testcase_22 AC 3 ms
6,944 KB
testcase_23 AC 3 ms
6,940 KB
testcase_24 AC 3 ms
6,944 KB
testcase_25 AC 2 ms
6,940 KB
testcase_26 AC 2 ms
6,940 KB
testcase_27 AC 3 ms
6,944 KB
testcase_28 AC 3 ms
6,940 KB
testcase_29 AC 2 ms
6,944 KB
testcase_30 AC 2 ms
6,944 KB
testcase_31 AC 2 ms
6,944 KB
testcase_32 AC 2 ms
6,944 KB
testcase_33 AC 2 ms
6,940 KB
testcase_34 AC 2 ms
6,940 KB
testcase_35 AC 2 ms
6,944 KB
testcase_36 AC 2 ms
6,940 KB
testcase_37 AC 2 ms
6,940 KB
testcase_38 AC 2 ms
6,940 KB
testcase_39 AC 14 ms
6,940 KB
testcase_40 AC 13 ms
6,940 KB
testcase_41 AC 14 ms
6,944 KB
testcase_42 AC 13 ms
6,944 KB
testcase_43 AC 13 ms
6,940 KB
testcase_44 AC 13 ms
6,940 KB
testcase_45 AC 14 ms
6,940 KB
testcase_46 AC 14 ms
6,944 KB
testcase_47 AC 13 ms
6,940 KB
testcase_48 AC 13 ms
6,944 KB
testcase_49 AC 49 ms
15,924 KB
testcase_50 AC 38 ms
17,368 KB
testcase_51 AC 39 ms
17,784 KB
testcase_52 AC 32 ms
13,968 KB
testcase_53 AC 37 ms
17,964 KB
testcase_54 AC 36 ms
16,676 KB
testcase_55 AC 58 ms
27,900 KB
testcase_56 AC 36 ms
16,608 KB
testcase_57 AC 35 ms
14,444 KB
testcase_58 AC 44 ms
23,872 KB
testcase_59 AC 13 ms
6,940 KB
testcase_60 AC 13 ms
6,944 KB
testcase_61 AC 14 ms
6,944 KB
testcase_62 AC 13 ms
6,940 KB
testcase_63 AC 14 ms
6,944 KB
testcase_64 AC 13 ms
6,944 KB
testcase_65 AC 14 ms
6,940 KB
testcase_66 AC 14 ms
6,944 KB
testcase_67 AC 13 ms
6,940 KB
testcase_68 AC 14 ms
6,940 KB
testcase_69 AC 94 ms
52,100 KB
testcase_70 AC 2 ms
6,944 KB
testcase_71 AC 2 ms
6,944 KB
testcase_72 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <unordered_map>
#define int long long
using namespace std;

class PalindromicTree {
private:
    vector<char> st;
    vector<int> length;
    vector<int> parent;
    vector<int> count;
    vector<unordered_map<char, int>> link;
    int lastNodeIdx;

public:
    PalindromicTree() {
        st.push_back(0);
        length.push_back(-1);
        length.push_back(0);
        parent.push_back(0);
        parent.push_back(0);
        count.push_back(0);
        count.push_back(0);
        link.push_back({});
        link.push_back({});
        lastNodeIdx = 0;
    }

    void add(char v) {
        st.push_back(v);
        int prevNodeIdx = lastNodeIdx;
        int nextNodeIdx = lastNodeIdx;
        while (true) {
            int head = length[nextNodeIdx] + 2;
            if (head <= st.size() && st[st.size() - head] == st.back()) {
                break;
            }
            nextNodeIdx = parent[nextNodeIdx];
        }
        int nextLen = length[nextNodeIdx] + 2;
        if (link[nextNodeIdx].count(v)) {
            lastNodeIdx = link[nextNodeIdx][v];
            count[lastNodeIdx]++;
            return;
        }
        lastNodeIdx = length.size();
        length.push_back(nextLen);
        if (nextLen == 1) {
            parent.push_back(1);
        } else {
            int prevNodeIdx = parent[nextNodeIdx];
            while (true) {
                int head = length[prevNodeIdx] + 2;
                if (head <= st.size() && st[st.size() - head] == st.back()) {
                    break;
                }
                prevNodeIdx = parent[prevNodeIdx];
            }
            parent.push_back(link[prevNodeIdx][v]);
        }
        count.push_back(1);
        link[nextNodeIdx][v] = lastNodeIdx;
        link.push_back({});
    }

    int maxScore() {
        int nodeCount = length.size();
        vector<int> score(nodeCount, 0);
        vector<int> tempCount = count;
        for (int i = nodeCount - 1; i >= 2; i--) {
            tempCount[parent[i]] += tempCount[i];
        }
        for (int i = 2; i < nodeCount; i++) {
            score[i] = score[parent[i]] + length[i] * tempCount[i];
        }
        int maxScore = 0;
        for (int s : score) {
            maxScore = max(maxScore, s);
        }
        return maxScore;
    }
};

signed main() {
    string s;
    cin >> s;
    PalindromicTree pt;
    for (int i = s.length() - 1; i >= 0; i--) {
        pt.add(s[i]);
    }
    cout << pt.maxScore() << endl;
    return 0;
}
0