Submission #932610

# Submission time Handle Problem Language Result Execution time Memory
932610 2024-02-23T21:41:10 Z jacobtpl Palindromes (APIO14_palindrome) C++17
100 / 100
36 ms 63160 KB
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <unordered_map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <vector>
using namespace std;

#define ll long long
#define pb push_back
#define mp make_pair
#define INF 1100000000
#define INFLL 1000000000000000000ll
#define ii pair<int,int>
#define sz(x) (int)(x).size()
#define all(x) (x).begin(),(x).end()
#define M1 1000000007ll
#define M2 1000000009ll
#define UQ(x) (x).resize(distance((x).begin(), unique(all(x))))
#define rep(i,a,b) for (int i = (a); i < (b); i++)
template<typename T> bool ckmax(T& a, T const& b) {return b>a?a=b,1:0;}
template<typename T> bool ckmin(T& a, T const& b) {return b<a?a=b,1:0;}


using vi = vector<int>;
const int maxn = 300005, sigma = 26;
 
struct paltree {
    string s = "$";
    int cur = 0;
    struct node {
        int len, link, occ=0;
        int to[26]={0};
        node(int _len, int _link):len(_len),link(_link){}
    };
    vector<node> st = {{0,1},{-1,0}};
    int last = 1;
    vector<vi> to;
    int get_link(int v) {
        while(s[sz(s) - st[v].len - 2] != s.back()) v = st[v].link;
        return v;
    }
    ll ans=0;
    void add_char(char C) {
        s += C;
        int c = C-'a';
        last = get_link(last);
        if (!st[last].to[c]) {
            st.emplace_back(st[last].len+2, st[get_link(st[last].link)].to[c]);
            st[last].to[c] = sz(st)-1;
        }
        last = st[last].to[c];
        st[last].occ++;
        // printf("update %d\n", last);
    }
    void dfs(int v) {
        for (int i=0;i<26;i++) {
            if (st[v].to[i]) {
                dfs(st[v].to[i]);
                st[v].occ += st[st[v].to[i]].occ;
            }
        }
        printf("v = %d:\n", v);
        for (int i=0;i<26;i++) {
            if (st[v].to[i]) {
                printf("has child %c: %d\n", i+'a', st[v].to[i]);
            }
        }
        printf("occ = %d, len = %d\n", st[v].occ, st[v].len);
        ans = max(ans, (ll)st[v].len * (ll)st[v].occ);
    }
    ll solve() {
        for (int i = sz(st)-1; i>=2; i--) {
            st[st[i].link].occ += st[i].occ;
        }
        for (auto &x:st) {
            ckmax(ans, (ll)x.len*(ll)x.occ);
        }
        return ans;
    }
};

int main() {
    cin.sync_with_stdio(0);
    cin.tie(0);
    string s;
    cin>>s;
    paltree p;
    for (int i=0;i<sz(s);i++) {
        p.add_char(s[i]);
    }
    cout << p.solve() << '\n';
}
// int dp[maxn][2]; // dp(i,b) = min number of palindromes split with parity b

// int main() {
//     scanf("%s",t);
//     int l = strlen(t);
//     init();
//     for (int i = 0; i < l; i++) {
//         add_letter(t[i]-'a');
//         if (i == 0) {
//             dp[0][0] = INF;
//             dp[0][1] = 1;
//         } else {
            
//         }
//     }
//     printf("%s\n", t);
// }
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 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 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 344 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 1 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 0 ms 348 KB Output is correct
23 Correct 0 ms 420 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 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 1 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 604 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
3 Correct 0 ms 604 KB Output is correct
4 Correct 0 ms 604 KB Output is correct
5 Correct 0 ms 604 KB Output is correct
6 Correct 0 ms 604 KB Output is correct
7 Correct 1 ms 600 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 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2360 KB Output is correct
2 Correct 1 ms 2364 KB Output is correct
3 Correct 1 ms 2364 KB Output is correct
4 Correct 1 ms 2364 KB Output is correct
5 Correct 2 ms 2364 KB Output is correct
6 Correct 2 ms 2364 KB Output is correct
7 Correct 1 ms 2364 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 1340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 17456 KB Output is correct
2 Correct 8 ms 16752 KB Output is correct
3 Correct 11 ms 16428 KB Output is correct
4 Correct 8 ms 17204 KB Output is correct
5 Correct 9 ms 15920 KB Output is correct
6 Correct 9 ms 16944 KB Output is correct
7 Correct 8 ms 17620 KB Output is correct
8 Correct 2 ms 860 KB Output is correct
9 Correct 4 ms 5996 KB Output is correct
10 Correct 8 ms 16796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 61468 KB Output is correct
2 Correct 25 ms 63060 KB Output is correct
3 Correct 29 ms 63160 KB Output is correct
4 Correct 25 ms 63064 KB Output is correct
5 Correct 26 ms 62800 KB Output is correct
6 Correct 24 ms 62516 KB Output is correct
7 Correct 20 ms 32776 KB Output is correct
8 Correct 5 ms 1764 KB Output is correct
9 Correct 5 ms 1764 KB Output is correct
10 Correct 18 ms 32348 KB Output is correct
11 Correct 24 ms 62584 KB Output is correct
12 Correct 7 ms 5512 KB Output is correct