답안 #838766

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
838766 2023-08-27T17:34:17 Z popovicirobert 회문 (APIO14_palindrome) C++14
73 / 100
157 ms 43836 KB
#include <bits/stdc++.h>

using namespace std;

constexpr int MOD1 = (int) 1e9 + 7;
constexpr int MOD2 = (int) 1e9 + 9;

struct Hash {
    int val1 = 0, val2 = 0;

    Hash operator*(int x) const {
        Hash h;
        h.val1 = (1LL * val1 * x) % MOD1;
        h.val2 = (1LL * val2 * x) % MOD2;
        return h; 
    }

    Hash operator*(const Hash& rhs) const {
        Hash h;
        h.val1 = (1LL * val1 * rhs.val1) % MOD1;
        h.val2 = (1LL * val2 * rhs.val2) % MOD2;
        return h; 
    }

    Hash operator-(const Hash& rhs) const {
        Hash h;
        h.val1 = (val1 - rhs.val1 + MOD1);
        if (h.val1 >= MOD1)
            h.val1 -= MOD1;

        h.val2 = (val2 - rhs.val2 + MOD2);
        if (h.val2 >= MOD2)
            h.val2 -= MOD2;
        return h; 
    }

    Hash operator+(int x) const {
        Hash h;
        h.val1 = val1 + x;
        if (h.val1 >= MOD1)
            h.val1 -= MOD1;

        h.val2 = val2 + x;
        if (h.val2 >= MOD2)
            h.val2 -= MOD2;
        return h;
    }

    Hash& operator=(int x) {
        val1 = val2 = x;
        return *this;
    }

    bool operator==(const Hash& rhs) const {
        return val1 == rhs.val1 && val2 == rhs.val2;
    }
};

namespace std {
    template<>
    struct hash<Hash> {
        size_t operator()(const Hash& h) const noexcept {
            return h.val1 ^ h.val2;
        }
    };
}

using ull = Hash;

constexpr int BASE = (int) 666013;
constexpr int MAXN = (int) 3e5;

char s[MAXN + 5];
ull pref_hash[MAXN + 5];
ull power[MAXN + 5];

inline ull Get(int l, int r) {
    return pref_hash[r] - pref_hash[l - 1] * power[r - l + 1];
}

char str[2 * MAXN + 5];
int len[2 * MAXN + 5];

int Build(char* s, int n) {
    power[0] = 1;
    for (int i = 1; i <= n; i++) {
        pref_hash[i] = pref_hash[i - 1] * BASE + (s[i] - 'a' + 1);
        power[i] = power[i - 1] * BASE;
    }

    int size = 0;
    str[++size] = '*';
    for (int i = 1; i <= n; i++) {
        str[++size] = s[i];
        str[++size] = '*';
    }

    return size;
}

void Manacher(char* str, int size) {
    int pos = 1;
    len[1] = 1;

    for (int i = 2; i <= size; i++) {
        len[i] = 1;
        if (pos + len[pos] >= i) {
            len[i] = min(pos + len[pos] - i, len[2 * pos - i]);
        }

        while (i - len[i] >= 1 && i + len[i] <= size && str[i - len[i]] == str[i + len[i]]) {
            len[i]++;
        }

        if (pos + len[pos] < i + len[i]) {
            pos = i;
        }
    }
}

unordered_map<ull, int> nodeId;
vector<vector<int>> g;

vector<int> nodeFreq(1);
vector<int> nodeLen(1);

void BuildGraph(int size) {

    int ID = 0;

    for (int i = 1; i <= size; i++) {
        int l = len[i];
        if (i % 2 == l % 2) {
            l--;
        }

        int prevId = 0;

        while (l > 0) {
            ull currHash;
            int mid = i / 2;

            if (i % 2 == 1) {
                currHash = Get(mid - l / 2 + 1, mid + l / 2);
            } else {
                currHash = Get(mid - l / 2, mid + l / 2);
            }

            int& currId = nodeId[currHash];
            if (currId == 0) {
                currId = ++ID;
                nodeLen.push_back(l);
                nodeFreq.push_back(0);
            }

            if (currId >= (int)g.size()) {
                g.resize(currId + 1);
            }

            if (prevId == 0) {
                nodeFreq[currId]++;
            } else {
                g[prevId].push_back(currId);
            }

            if (currId != ID) {
                break;
            }

            prevId = currId;
            l -= 2;
        }
    }
}


int main() {
#ifdef HOME
    ifstream cin("input.in");
    ofstream cout("output.out");
#endif
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    cin >> (s + 1);
    int n = strlen(s + 1);

    int size = Build(s, n);

    Manacher(str, size);

    // for (int i = 1; i <= size; i++) {
    //     cerr << (char)str[i] << " ";
    // }
    // cerr << "\n";
    // for (int i = 1; i <= size; i++) {
    //     cerr << len[i] << " ";
    // }
    // cerr << "\n";

    BuildGraph(size);

    int nodes = (int)g.size() - 1;
    vector<int> order(nodes);
    iota(order.begin(), order.end(), 1);
    sort(order.begin(), order.end(), [](const int& a, const int& b) {
        return nodeLen[a] > nodeLen[b];
    });

    long long answer = 0;
    for (auto node : order) {
        // cerr << nodeFreq[node] << " " << nodeLen[node] << "\n";
        answer = max(answer, 1LL * nodeFreq[node] * nodeLen[node]);
        for (auto son : g[node]) {
            nodeFreq[son] += nodeFreq[node];
        }
    }

    cout << answer;

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 0 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 0 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 292 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 1748 KB Output is correct
2 Correct 3 ms 1748 KB Output is correct
3 Correct 3 ms 1748 KB Output is correct
4 Correct 3 ms 1748 KB Output is correct
5 Correct 3 ms 1748 KB Output is correct
6 Correct 3 ms 1748 KB Output is correct
7 Correct 3 ms 1748 KB Output is correct
8 Correct 2 ms 596 KB Output is correct
9 Correct 2 ms 596 KB Output is correct
10 Correct 2 ms 1364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 14040 KB Output is correct
2 Correct 36 ms 14152 KB Output is correct
3 Correct 33 ms 14076 KB Output is correct
4 Correct 36 ms 14172 KB Output is correct
5 Correct 32 ms 14084 KB Output is correct
6 Correct 29 ms 11804 KB Output is correct
7 Correct 28 ms 11972 KB Output is correct
8 Correct 16 ms 3108 KB Output is correct
9 Correct 19 ms 5584 KB Output is correct
10 Correct 26 ms 11888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 140 ms 43796 KB Output is correct
2 Correct 144 ms 43836 KB Output is correct
3 Correct 155 ms 43732 KB Output is correct
4 Correct 157 ms 43812 KB Output is correct
5 Correct 154 ms 43832 KB Output is correct
6 Correct 130 ms 43760 KB Output is correct
7 Correct 123 ms 35372 KB Output is correct
8 Incorrect 50 ms 8324 KB Output isn't correct
9 Halted 0 ms 0 KB -