답안 #830743

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
830743 2023-08-19T10:00:59 Z Koyote 회문 (APIO14_palindrome) C++14
100 / 100
93 ms 80992 KB
#include <bits/stdc++.h>
using namespace std;

struct palin_tree {
    struct node {
        int len, suffix, serial_link = 0, diff = 0, occ = 0;
        vector<int> edges;
        map<int, int> nxt; // use this if n <= 1e6 and memory <= 256MB
        node(int _len = 0, int _suffix = 0) : len(_len), suffix(_suffix) {}
    };

    vector<node> tree;
    vector<char> past;
    int turtle;

    palin_tree(int expected_size = int(1e6)) : tree(2, node()), past(1, -1) {
        tree.reserve(expected_size), past.reserve(expected_size);
        tree[0].suffix = 1, tree[1].len = -1;
        turtle = 0;
        tree[1].edges.push_back(0);
    }

    int go_back(int u) {
        while (past[(int)past.size() - tree[u].len - 2] != past.back()) 
            u = tree[u].suffix;
        return u;
    }

    void append(int c) {
        past.push_back(c);
        turtle = go_back(turtle);
        if (!tree[turtle].nxt[c]) {
            node tmp; tmp.len = tree[turtle].len + 2;
            tmp.suffix = tree[go_back(tree[turtle].suffix)].nxt[c];
            tmp.diff = tmp.len - tree[tmp.suffix].len;
            tmp.serial_link = (tmp.diff != tree[tmp.suffix].diff ?
                tmp.suffix : tree[tmp.suffix].serial_link);
            tmp.occ = 1;
            
            tree[turtle].nxt[c] = (int)tree.size();
            tree[tmp.suffix].edges.push_back((int)tree.size());
            tree.push_back(tmp);
        } else tree[tree[turtle].nxt[c]].occ++;
        turtle = tree[turtle].nxt[c];
    }

    long long solve(int u = 1) {
        long long res = 0;
        for (int v : tree[u].edges) {
            res = max(res, solve(v));
            tree[u].occ += tree[v].occ;
        }
        return max(res, 1LL * tree[u].occ * tree[u].len);
    }
};

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    string s; s.reserve(int(3e5)); cin >> s;
    palin_tree ptree(s.size());
    for (auto c : s) ptree.append(c - 'a');
    cout << ptree.solve() << '\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 336 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 0 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 0 ms 336 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 260 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 1 ms 340 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 0 ms 336 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 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 596 KB Output is correct
2 Correct 1 ms 596 KB Output is correct
3 Correct 1 ms 596 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 464 KB Output is correct
8 Correct 1 ms 596 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 340 KB Output is correct
12 Correct 1 ms 464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3032 KB Output is correct
2 Correct 2 ms 3028 KB Output is correct
3 Correct 3 ms 3028 KB Output is correct
4 Correct 2 ms 3028 KB Output is correct
5 Correct 2 ms 2772 KB Output is correct
6 Correct 2 ms 2900 KB Output is correct
7 Correct 3 ms 2132 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 2 ms 1364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 27120 KB Output is correct
2 Correct 24 ms 27220 KB Output is correct
3 Correct 27 ms 27204 KB Output is correct
4 Correct 26 ms 27136 KB Output is correct
5 Correct 28 ms 25220 KB Output is correct
6 Correct 13 ms 12764 KB Output is correct
7 Correct 16 ms 15400 KB Output is correct
8 Correct 6 ms 852 KB Output is correct
9 Correct 9 ms 5460 KB Output is correct
10 Correct 19 ms 13660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 77 ms 80984 KB Output is correct
2 Correct 61 ms 54248 KB Output is correct
3 Correct 72 ms 80992 KB Output is correct
4 Correct 70 ms 80928 KB Output is correct
5 Correct 93 ms 76196 KB Output is correct
6 Correct 55 ms 47996 KB Output is correct
7 Correct 50 ms 44368 KB Output is correct
8 Correct 17 ms 1732 KB Output is correct
9 Correct 18 ms 1656 KB Output is correct
10 Correct 60 ms 39716 KB Output is correct
11 Correct 73 ms 80960 KB Output is correct
12 Correct 29 ms 6956 KB Output is correct