Submission #1008034

# Submission time Handle Problem Language Result Execution time Memory
1008034 2024-06-26T06:28:57 Z Zero_OP Palindromes (APIO14_palindrome) C++14
100 / 100
65 ms 59212 KB
#include<bits/stdc++.h>

using namespace std;
using int64 = int64_t;

#define  FOR(i, l, r) for(int i = (l), _r = (r); i <= _r; ++i)
#define FORD(i, r, l) for(int i = (r), _l = (l); i >= _l; --i)
#define  rep(i, l, r) for(int i = (l), _r = (r); i < _r; ++i)
#define repd(i, r, l) for(int i = (r) - 1, _l = l; i >= _l; --i)
#define          left __left
#define         right __right
#define          next __next
#define          prev __prev
#define           div __div
#define            pb push_back
#define            pf push_front
#define        all(v) v.begin(), v.end()
#define         sz(v) (int)v.size()
#define    compact(v) v.erase(unique(all(v)), end(v))
#define        dbg(v) "[" #v " = " << (v) << "]"
#define    file(name) if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }

template<typename T>
    bool minimize(T& a, const T& b){
        if(a > b) return a = b, true;
        return false;
    }

template<typename T>
    bool maximize(T& a, const T& b){
        if(a < b) return a = b, true;
        return false;
    }

template<int dimension, typename T>
struct vec : public vector<vec<dimension - 1, T>> {
    static_assert(dimension > 0, "Dimension must be positive !\n");
    template<typename... Args>
    vec(int n = 0, Args... args) : vector<vec<dimension - 1, T>> (n, vec<dimension - 1, T>(args...)) {}
};

template<typename T>
struct vec<1, T> : public vector<T> {
    vec(int n = 0, T val = T()) : vector<T>(n, val) {}
};

void preprocess();
void init();
void process();

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

    file("antuvu");
    preprocess();

    int T = 1; //cin >> T;
    while(T--){
        init();
        process();
    }

    return 0;
}

// Palindrome Tree {{{
// Notes:
// - number of *distinct* palindrome substring <= N
// Tested:
// - https://oj.vnoi.info/problem/icpc22_mn_d
template<int MAXC = 26>
struct PalindromicTree {
    PalindromicTree(const string& str)
            : _sz(str.size() + 5),
              next(_sz, vector<int> (MAXC, 0)),
              link(_sz, 0), qlink(_sz, 0),
              cnt(_sz, 0), right_id(_sz, 0),
              len(_sz, 0), s(_sz, 0) {
        init();
        for (int i = 0; i < (int) str.size(); ++i) {
            add(str[i], i);
        }
        count();
    }
    int _sz;

    // returns vector of (left, right, frequency)
    vector<tuple<int,int,int>> get_palindromes() {
        vector<tuple<int,int,int>> res;
        dfs(0, res);
        dfs(1, res);
        return res;
    }

    void dfs(int u, vector<tuple<int,int,int>>& res) {
        if (u > 1) {  // u = 0 and u = 1 are two empty nodes
            res.emplace_back(right_id[u] - len[u] + 1, right_id[u], cnt[u]);
        }
        for (int i = 0; i < MAXC; ++i) {
            if (next[u][i]) dfs(next[u][i], res);
        }
    }

    int last, n, p;
    vector<vector<int>> next, dlink;
    vector<int> link, qlink, cnt, right_id, len, s;

    int newnode(int l, int right) {
        len[p] = l;
        right_id[p] = right;
        return p++;
    }
    void init() {
        p = 0;
        newnode(0, -1), newnode(-1, -1);
        n = last = 0;
        s[n] = -1, link[0] = 1;
    }
    int getlink(int x) {
        while (s[n - len[x] - 1] != s[n]) {
            if (s[n - len[link[x]] - 1] == s[n]) x = link[x];
            else x = qlink[x];
        }
        return x;
    }
    void add(char c, int right) {
        c -= 'a';
        s[++n] = c;
        int cur = getlink(last);
        if (!next[cur][(int) c]) {
            int now = newnode(len[cur] + 2, right);
            link[now] = next[getlink(link[cur])][(int) c];
            next[cur][(int) c] = now;
            if (s[n - len[link[now]]] == s[n - len[link[link[now]]]]) {
                qlink[now] = qlink[link[now]];
            }
            else {
                qlink[now] = link[link[now]];
            }
        }
        last = next[cur][(int) c];
        cnt[last]++;
    }
    void count() {
        for (int i = p - 1; i >= 0; i--) {
            cnt[link[i]] += cnt[i];
        }
    }
};
// }}}

void preprocess(){

}

void init(){

}

void process(){
    string s;
    cin >> s;

    PalindromicTree<26> T(s);
    vector<tuple<int, int, int>> p = T.get_palindromes();
    long long ans = 0;
    for(int i = 0; i < sz(p); ++i){
        int l, r, cnt;
        tie(l, r, cnt) = p[i];

        maximize(ans, 1ll * cnt * (r - l + 1));
    }
    cout << ans << '\n';
}

Compilation message

palindrome.cpp: In function 'int main()':
palindrome.cpp:21:58: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   21 | #define    file(name) if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
palindrome.cpp:55:5: note: in expansion of macro 'file'
   55 |     file("antuvu");
      |     ^~~~
palindrome.cpp:21:91: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   21 | #define    file(name) if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                                                    ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
palindrome.cpp:55:5: note: in expansion of macro 'file'
   55 |     file("antuvu");
      |     ^~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 452 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 600 KB Output is correct
5 Correct 1 ms 600 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 0 ms 604 KB Output is correct
8 Correct 0 ms 604 KB Output is correct
9 Correct 0 ms 604 KB Output is correct
10 Correct 0 ms 604 KB Output is correct
11 Correct 0 ms 604 KB Output is correct
12 Correct 0 ms 600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2392 KB Output is correct
2 Correct 3 ms 2392 KB Output is correct
3 Correct 2 ms 2396 KB Output is correct
4 Correct 2 ms 2396 KB Output is correct
5 Correct 2 ms 2396 KB Output is correct
6 Correct 2 ms 2396 KB Output is correct
7 Correct 1 ms 2336 KB Output is correct
8 Correct 1 ms 1880 KB Output is correct
9 Correct 1 ms 1884 KB Output is correct
10 Correct 1 ms 2140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 19140 KB Output is correct
2 Correct 16 ms 18384 KB Output is correct
3 Correct 15 ms 19040 KB Output is correct
4 Correct 21 ms 18640 KB Output is correct
5 Correct 17 ms 18508 KB Output is correct
6 Correct 15 ms 18128 KB Output is correct
7 Correct 14 ms 18144 KB Output is correct
8 Correct 10 ms 16220 KB Output is correct
9 Correct 11 ms 16980 KB Output is correct
10 Correct 15 ms 18644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 58 ms 58304 KB Output is correct
2 Correct 48 ms 56648 KB Output is correct
3 Correct 43 ms 58960 KB Output is correct
4 Correct 42 ms 56908 KB Output is correct
5 Correct 49 ms 58700 KB Output is correct
6 Correct 51 ms 56464 KB Output is correct
7 Correct 42 ms 51792 KB Output is correct
8 Correct 25 ms 47840 KB Output is correct
9 Correct 25 ms 47832 KB Output is correct
10 Correct 41 ms 54092 KB Output is correct
11 Correct 65 ms 59212 KB Output is correct
12 Correct 26 ms 48816 KB Output is correct