Submission #418827

# Submission time Handle Problem Language Result Execution time Memory
418827 2021-06-06T01:09:02 Z Lam_lai_cuoc_doi Palindromes (APIO14_palindrome) C++17
100 / 100
119 ms 95748 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;

constexpr bool typetest = 0;
constexpr int N = 3e5 + 2;
constexpr ll Inf = 1e17;

struct node
{
    node *child[26], *sufflink;
    vector<node *> adj;
    int len, cnt;

    node()
    {
        len = 0;
        cnt = 0;
        for (int i = 0; i < 26; ++i)
            child[i] = NULL;
        sufflink = NULL;
    }
};

struct PalindromeTree
{
    node even, odd;
    PalindromeTree()
    {
        odd.len = -1;
        odd.sufflink = &odd;
        even.sufflink = &odd;
        odd.adj.emplace_back(&even);
        even.len = 0;
    }

    void Assign(string &s)
    {
        node *last = &even;
        for (int i = 0; i < (int)s.size(); ++i)
        {
            node *tmp = last;

            while (s[i - tmp->len - 1] != s[i])
                tmp = tmp->sufflink;

            if (tmp->child[s[i] - 'a'])
            {
                last = tmp->child[s[i] - 'a'];
                ++last->cnt;
                continue;
            }

            last = tmp->child[s[i] - 'a'] = new node;
            last->len = tmp->len + 2;
            ++last->cnt;

            if (last->len == 1)
            {
                last->sufflink = &even;
                even.adj.emplace_back(last);
                continue;
            }

            do
            {
                tmp = tmp->sufflink;
            } while (s[i - tmp->len - 1] != s[i]);

            last->sufflink = tmp->child[s[i] - 'a'];
            last->sufflink->adj.emplace_back(last);
        }
    }

    ll dfs(node *v)
    {
        ll res(-Inf);
        for (auto i : v->adj)
        {
            res = max(res, dfs(i));
            v->cnt += i->cnt;
        }
        return max(res, 1ll * v->len * v->cnt);
    }
} f;

string s;

void Read()
{
    cin >> s;
}

void Solve()
{
    f.Assign(s);
    cout << f.dfs(&f.odd);
}

int32_t main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t(1);
    if (typetest)
        cin >> t;
    for (int _ = 1; _ <= t; ++_)
    {
        Read();
        Solve();
    }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 312 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 312 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 316 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 588 KB Output is correct
2 Correct 1 ms 588 KB Output is correct
3 Correct 1 ms 588 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 588 KB Output is correct
6 Correct 1 ms 588 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 1 ms 588 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 3276 KB Output is correct
2 Correct 3 ms 3276 KB Output is correct
3 Correct 3 ms 3404 KB Output is correct
4 Correct 3 ms 3404 KB Output is correct
5 Correct 3 ms 3020 KB Output is correct
6 Correct 3 ms 3020 KB Output is correct
7 Correct 3 ms 3148 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 2 ms 2124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 30404 KB Output is correct
2 Correct 29 ms 29636 KB Output is correct
3 Correct 28 ms 32076 KB Output is correct
4 Correct 29 ms 30756 KB Output is correct
5 Correct 39 ms 27196 KB Output is correct
6 Correct 25 ms 20592 KB Output is correct
7 Correct 25 ms 24512 KB Output is correct
8 Correct 2 ms 716 KB Output is correct
9 Correct 8 ms 7756 KB Output is correct
10 Correct 32 ms 23144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 84 ms 90604 KB Output is correct
2 Correct 84 ms 86800 KB Output is correct
3 Correct 95 ms 95748 KB Output is correct
4 Correct 81 ms 89164 KB Output is correct
5 Correct 119 ms 81608 KB Output is correct
6 Correct 79 ms 77152 KB Output is correct
7 Correct 73 ms 71536 KB Output is correct
8 Correct 5 ms 1236 KB Output is correct
9 Correct 5 ms 1236 KB Output is correct
10 Correct 87 ms 66620 KB Output is correct
11 Correct 110 ms 87112 KB Output is correct
12 Correct 13 ms 9812 KB Output is correct