Submission #231280

# Submission time Handle Problem Language Result Execution time Memory
231280 2020-05-13T09:55:45 Z lyc Palindromes (APIO14_palindrome) C++14
100 / 100
221 ms 126104 KB
#include <bits/stdc++.h>
using namespace std;

#define TRACE(x) cerr << #x << " :: " << x << endl
#define _ << " " <<
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(),(x).end()
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for (int i=(a);i>=(b);--i)
using ll=long long;

const int mxN = 600010;
const int lgN = 20;

struct trie {
    map<int,int> nex[mxN];
    int pa[mxN][lgN], v[mxN], cnt;

    trie() {
        cnt = 1;
        v[0] = 0;
        FOR(k,0,lgN-1) pa[0][k] = -1;
    }

    int make(int fa) {
        int u = cnt++;
        v[u] = 0;
        pa[u][0] = fa;
        FOR(k,1,lgN-1) pa[u][k] = (pa[u][k-1] == -1 ? -1 : pa[pa[u][k-1]][k-1]);
        return u;
    }

    int nthPa(int u, int n) {
        RFOR(k,lgN-1,0) if (n&(1<<k)) {
            if (u == -1) break;
            else u = pa[u][k];
        }
        return u;
    }

    int get(int u, char c) {
        if (!nex[u].count(c-'a')) {
            nex[u][c-'a'] = make(u);
        }
        return nex[u][c-'a'];
    }

    ll ans(int u, int d) {
        //TRACE(d _ v);
        ll r = 0;
        for (auto& c : nex[u]) {
            r = max(r, ans(c.second,d+1));
            v[u] += v[c.second];
        }
        r = max(r, (ll)d*v[u]);
        return r;
    }
} root;

int cur[mxN];

string S, T;
int N, P[mxN];

string preprocess(string S) {
    string T = "{";
    for (auto& c : S) {
        T += "|";
        T += c;
    }
    T += "|}";
    return T;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin >> S;
    T = preprocess(S);
    N = SZ(T);

    int m = 0, r = 0;
    fill_n(P,N,0);
    FOR(i,1,N-2){
        int _i = m - (i-m);
        if (r > i) {
            P[i] = min(P[_i], r-i);
            cur[i] = root.nthPa(cur[_i],P[_i]-P[i]);
        } else {
            cur[i] = root.get(0,T[i]);
        }
        while (T[i+P[i]+1] == T[i-P[i]-1]) {
            cur[i] = root.get(cur[i],T[i+P[i]+1]);
            ++P[i];
        }
        ++root.v[cur[i]];
        if (i+P[i] > r) m = i, r = i+P[i];
    }
    cout << root.ans(0,-1) << '\n';
    //cout << T << '\n';
    //FOR(i,0,N-1) cout << P[i] << ' ';
    //cout << endl;
    //root->dbg("",-1);
}

# Verdict Execution time Memory Grader output
1 Correct 18 ms 28544 KB Output is correct
2 Correct 19 ms 28544 KB Output is correct
3 Correct 18 ms 28544 KB Output is correct
4 Correct 18 ms 28544 KB Output is correct
5 Correct 18 ms 28672 KB Output is correct
6 Correct 19 ms 28544 KB Output is correct
7 Correct 18 ms 28544 KB Output is correct
8 Correct 19 ms 28544 KB Output is correct
9 Correct 19 ms 28544 KB Output is correct
10 Correct 18 ms 28544 KB Output is correct
11 Correct 19 ms 28544 KB Output is correct
12 Correct 18 ms 28544 KB Output is correct
13 Correct 19 ms 28544 KB Output is correct
14 Correct 18 ms 28544 KB Output is correct
15 Correct 18 ms 28544 KB Output is correct
16 Correct 18 ms 28544 KB Output is correct
17 Correct 18 ms 28544 KB Output is correct
18 Correct 19 ms 28544 KB Output is correct
19 Correct 18 ms 28596 KB Output is correct
20 Correct 18 ms 28544 KB Output is correct
21 Correct 18 ms 28544 KB Output is correct
22 Correct 19 ms 28544 KB Output is correct
23 Correct 19 ms 28544 KB Output is correct
24 Correct 18 ms 28544 KB Output is correct
25 Correct 19 ms 28544 KB Output is correct
26 Correct 19 ms 28544 KB Output is correct
27 Correct 18 ms 28544 KB Output is correct
28 Correct 18 ms 28544 KB Output is correct
29 Correct 18 ms 28544 KB Output is correct
30 Correct 19 ms 28544 KB Output is correct
31 Correct 18 ms 28544 KB Output is correct
32 Correct 20 ms 28544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 28800 KB Output is correct
2 Correct 19 ms 28800 KB Output is correct
3 Correct 19 ms 28800 KB Output is correct
4 Correct 26 ms 28800 KB Output is correct
5 Correct 20 ms 28800 KB Output is correct
6 Correct 19 ms 28800 KB Output is correct
7 Correct 19 ms 28928 KB Output is correct
8 Correct 19 ms 28928 KB Output is correct
9 Correct 19 ms 28800 KB Output is correct
10 Correct 19 ms 28544 KB Output is correct
11 Correct 19 ms 28544 KB Output is correct
12 Correct 19 ms 28800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 31744 KB Output is correct
2 Correct 22 ms 31616 KB Output is correct
3 Correct 23 ms 31744 KB Output is correct
4 Correct 24 ms 31744 KB Output is correct
5 Correct 23 ms 31744 KB Output is correct
6 Correct 27 ms 31616 KB Output is correct
7 Correct 24 ms 31488 KB Output is correct
8 Correct 19 ms 28800 KB Output is correct
9 Correct 21 ms 28800 KB Output is correct
10 Correct 22 ms 30720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 77 ms 60884 KB Output is correct
2 Correct 73 ms 58904 KB Output is correct
3 Correct 73 ms 60952 KB Output is correct
4 Correct 70 ms 59288 KB Output is correct
5 Correct 68 ms 59596 KB Output is correct
6 Correct 54 ms 50712 KB Output is correct
7 Correct 57 ms 53144 KB Output is correct
8 Correct 29 ms 30880 KB Output is correct
9 Correct 38 ms 37528 KB Output is correct
10 Correct 61 ms 55320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 203 ms 125972 KB Output is correct
2 Correct 190 ms 115860 KB Output is correct
3 Correct 194 ms 126104 KB Output is correct
4 Correct 173 ms 117008 KB Output is correct
5 Correct 182 ms 124180 KB Output is correct
6 Correct 154 ms 106516 KB Output is correct
7 Correct 148 ms 101652 KB Output is correct
8 Correct 48 ms 34836 KB Output is correct
9 Correct 49 ms 34836 KB Output is correct
10 Correct 156 ms 110100 KB Output is correct
11 Correct 221 ms 125972 KB Output is correct
12 Correct 59 ms 43028 KB Output is correct