Submission #677933

# Submission time Handle Problem Language Result Execution time Memory
677933 2023-01-04T16:43:19 Z Romiros Palindromes (APIO14_palindrome) C++17
100 / 100
954 ms 59676 KB
#include <bits/stdc++.h>
 
#define ll long long
#define ld long double
#define vi vector<int>
#define pii pair<int, int>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
 
#pragma GCC optimize("O3", "unroll-loops")
#pragma GCC target("avx2", "popcnt")

using namespace std;
 
const int MAXN = 3e5 + 10;
 
inline vector<int> build_sa(string s){
    s+='$';
    int n= s.size();
    vector<int>p(n),pn(n),cn(n),c(n),cnt(n);
    {
        vector<pii>a(n);
        for(int i=0;i<n;i++) a[i]={s[i]-'a',i};
        sort(all(a));
        for(int i=0;i<n;i++) p[i]=a[i].second;
        c[p[0]]=0;
        for(int i=1;i<n;i++){
            if(a[i].first ==a[i-1].first ){
                c[p[i]]=c[p[i-1]];
            }
            else{
                c[p[i]]=c[p[i-1]]+1;
            }
        }
    }
    int k=0;
    while((1 << k)<n){
        cnt.assign(n,0);
        for(int i=0;i<n;i++){
            pn[i]=p[i]-(1 << k);
            if(pn[i]<0) pn[i]+=n;
        }
        for(auto &z : c) cnt[z]++;
        for(int i=1;i<n;i++) cnt[i]+=cnt[i-1];
        for(int i=n-1;i>=0;i--){
            --cnt[c[pn[i]]];
            p[cnt[c[pn[i]]]]=pn[i];
        }
        cn[p[0]]=0;
        for(int i=1;i<n;i++){
            int f=(p[i]+(1 << k))%n,s=(p[i-1]+(1 << k))%n;
            if(c[p[i]]!=c[p[i-1]] || c[f]!=c[s]){
                cn[p[i]]=cn[p[i-1]]+1;
            }else cn[p[i]]=cn[p[i-1]];
        }
        c=cn;
        k++;
    }
    return p;
}
 
inline vector<int> build_lcp(const vector<int>& p, const string& s){
    int n = s.size();
    vector<int> pos(n + 1);
    for (int i = 0; i <= n; i++){
        pos[p[i]] = i;
    }
    vector<int> lcp(n);
    int k = 0;
    for (int i = 0; i < (n - 1); i++){
        while (s[i + k] == s[p[pos[i] - 1] + k]){
            k++;
        }
        lcp[pos[i] - 1] = k;
        k -= (k != 0);
    }
    return lcp;
}

struct SparseTable{
    vector<vector<int>> st;

    SparseTable(vector<int> a){
        int n = a.size();
        st.resize(n);
        for (int i = 0; i < n; i++){
            st[i].push_back(a[i]);
        }
        for (int k = 1; k <= n; k *= 2){
            for (int i = 0; i + k < n; i++){
                st[i].push_back(min(st[i].back(), st[i + k].back()));
            }
        }
    }

    inline int log2(int x){
        return 31 - __builtin_clz(x);
    }

    inline int get(int l, int r){
        if (l > r){
            return 1e9;
        }
        int k = log2(r - l + 1);
        return min(st[l][k], st[r - (1 << k) + 1][k]);
    }
};

SparseTable sp({});
vector<int> d(MAXN);
vector<int> pos;
string s;

inline int lcp(int i, int j){
    int l = pos[i];
    int r = pos[j];
    if (l > r){
        swap(l, r);
    }
    return min({d[i], d[j], sp.get(l, r - 1)});
}

inline int cmp(int i, int j){
    int l = lcp(i, j);
    if (l == min(d[i], d[j])){
        return d[i] < d[j];
    }
    return s[i - l] < s[j - l];
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    #ifdef ON_PC
        freopen("input.txt", "r", stdin);
    #endif
    // freopen("output.txt", "w", stdout);
    int T = 1;
    // cin >> T;
    while (T--){
        cin >> s;
        int n = s.size();   
        vector<int> sa = build_sa(s);
        vector<int> lcpv = build_lcp(sa, s);
        pos = vector<int>(n + 2);
        for (int i = 0; i < sa.size(); i++){
            pos[sa[i] + 1] = i;
        }
        sp = SparseTable(lcpv);
        s = " " + s;
        ll res = 0;
        vector<int> order(n);
        vector<int> lb(n);
        vector<int> rb(n);
        vector<int> st;
        vector<int> lcp_res(n);
        for (int f = 0; f < 2; f++){
            d.assign(n + 1, 0);
            for (int i = 1, l = 0, r = -1; i <= n; i++){
                if (i + f <= r){
                    d[i] = min(r - (i + f) + 1, d[l + r - (i + f)]);
                }
                while (i - d[i] >= 1 && i + f + d[i] <= n && 
                       s[i - d[i]] == s[i + f + d[i]]){
                           d[i]++;
                }
                if (i + f + d[i] - 1 > r){
                    r = i + f + d[i] - 1;
                    l = i - d[i] + 1;
                }
            }
            for (int i = 1; i <= n; i++){
                res = max(res, (ll)(2 * d[i] - !f));
            }
            for (int i = 1; i <= n; i++){
                order[i - 1] = i;
            }
            sort(all(order), cmp);
            for (int i = 0; i + 1 < n; i++){
                lcp_res[i] = lcp(order[i], order[i + 1]);
            }
            for (int i = 0; i < n; i++){
                while (!st.empty() && lcp_res[st.back()] >= lcp_res[i]){
                    st.pop_back();
                }
                lb[i] = 0;
                if (!st.empty()){
                    lb[i] = st.back() + 1;
                }
                st.push_back(i);
            }
            st.clear();
            for (int i = n - 1; i >= 0; i--){
                while (!st.empty() && lcp_res[st.back()] >= lcp_res[i]){
                    st.pop_back();
                }
                rb[i] = n - 1;
                if (!st.empty()){
                    rb[i] = st.back();
                }
                st.push_back(i);
            }
            st.clear();
            for (int i = 0; i < n; i++){
                res = max(res, (2 * lcp_res[i] - !f) * 1ll * (rb[i] - lb[i] + 1));
            }
        }
        cout << res << "\n";
    }
    return 0;
}

Compilation message

palindrome.cpp: In function 'int main()':
palindrome.cpp:147:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  147 |         for (int i = 0; i < sa.size(); i++){
      |                         ~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1492 KB Output is correct
2 Correct 1 ms 1492 KB Output is correct
3 Correct 1 ms 1492 KB Output is correct
4 Correct 1 ms 1492 KB Output is correct
5 Correct 1 ms 1492 KB Output is correct
6 Correct 1 ms 1492 KB Output is correct
7 Correct 1 ms 1492 KB Output is correct
8 Correct 1 ms 1492 KB Output is correct
9 Correct 1 ms 1492 KB Output is correct
10 Correct 1 ms 1492 KB Output is correct
11 Correct 1 ms 1620 KB Output is correct
12 Correct 1 ms 1492 KB Output is correct
13 Correct 1 ms 1492 KB Output is correct
14 Correct 1 ms 1492 KB Output is correct
15 Correct 2 ms 1508 KB Output is correct
16 Correct 1 ms 1492 KB Output is correct
17 Correct 1 ms 1492 KB Output is correct
18 Correct 1 ms 1492 KB Output is correct
19 Correct 2 ms 1492 KB Output is correct
20 Correct 2 ms 1492 KB Output is correct
21 Correct 2 ms 1504 KB Output is correct
22 Correct 2 ms 1492 KB Output is correct
23 Correct 2 ms 1492 KB Output is correct
24 Correct 1 ms 1504 KB Output is correct
25 Correct 1 ms 1504 KB Output is correct
26 Correct 1 ms 1492 KB Output is correct
27 Correct 1 ms 1492 KB Output is correct
28 Correct 1 ms 1492 KB Output is correct
29 Correct 1 ms 1492 KB Output is correct
30 Correct 1 ms 1492 KB Output is correct
31 Correct 1 ms 1492 KB Output is correct
32 Correct 1 ms 1496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1620 KB Output is correct
2 Correct 2 ms 1632 KB Output is correct
3 Correct 2 ms 1620 KB Output is correct
4 Correct 2 ms 1632 KB Output is correct
5 Correct 2 ms 1620 KB Output is correct
6 Correct 2 ms 1632 KB Output is correct
7 Correct 2 ms 1620 KB Output is correct
8 Correct 2 ms 1620 KB Output is correct
9 Correct 2 ms 1620 KB Output is correct
10 Correct 2 ms 1636 KB Output is correct
11 Correct 2 ms 1620 KB Output is correct
12 Correct 2 ms 1620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 2792 KB Output is correct
2 Correct 12 ms 2784 KB Output is correct
3 Correct 14 ms 2892 KB Output is correct
4 Correct 15 ms 2880 KB Output is correct
5 Correct 11 ms 2752 KB Output is correct
6 Correct 12 ms 2816 KB Output is correct
7 Correct 11 ms 2880 KB Output is correct
8 Correct 12 ms 2752 KB Output is correct
9 Correct 12 ms 2888 KB Output is correct
10 Correct 12 ms 2816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 138 ms 19364 KB Output is correct
2 Correct 132 ms 19364 KB Output is correct
3 Correct 204 ms 19552 KB Output is correct
4 Correct 164 ms 19248 KB Output is correct
5 Correct 256 ms 19232 KB Output is correct
6 Correct 220 ms 19228 KB Output is correct
7 Correct 161 ms 19228 KB Output is correct
8 Correct 246 ms 19224 KB Output is correct
9 Correct 200 ms 19232 KB Output is correct
10 Correct 207 ms 19124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 458 ms 58968 KB Output is correct
2 Correct 510 ms 58468 KB Output is correct
3 Correct 701 ms 59676 KB Output is correct
4 Correct 529 ms 58464 KB Output is correct
5 Correct 954 ms 58476 KB Output is correct
6 Correct 588 ms 58384 KB Output is correct
7 Correct 622 ms 58472 KB Output is correct
8 Correct 942 ms 58512 KB Output is correct
9 Correct 844 ms 58512 KB Output is correct
10 Correct 843 ms 58432 KB Output is correct
11 Correct 563 ms 58468 KB Output is correct
12 Correct 790 ms 58512 KB Output is correct