Submission #947189

# Submission time Handle Problem Language Result Execution time Memory
947189 2024-03-15T15:43:12 Z steveonalex Palindromes (APIO14_palindrome) C++14
100 / 100
82 ms 126804 KB
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
 
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
 
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
 
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ll mask){return __builtin_ctzll(mask);}
int logOf(ll mask){return 63 - __builtin_clzll(mask);}
 
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
 
template <class T1, class T2>
    bool maximize(T1 &a, T2 b){
        if (a < b) {a = b; return true;}
        return false;
    }
 
template <class T1, class T2>
    bool minimize(T1 &a, T2 b){
        if (a > b) {a = b; return true;}
        return false;
    }
 
template <class T>
    void printArr(T& container, string separator = " ", string finish = "\n", ostream &out = cout){
        for(auto item: container) out << item << separator;
        out << finish;
    }
 
template <class T>
    void remove_dup(vector<T> &a){
        sort(ALL(a));
        a.resize(unique(ALL(a)) - a.begin());
    }
 
const long K = 26;
 
struct PalindromeTree{
    struct Node{
        long child[K], layer, pi;
        Node(long _layer = 0){
            memset(child, -1, sizeof child);
            layer = _layer;
            pi = -1;
        }
    };
 
    vector<Node> a;
    vector<int> visit_cnt;
 
    PalindromeTree(){
        a.push_back(Node(-1)); a.push_back(Node(0));
        visit_cnt.resize(3e5 + 69);
        a[1].pi = a[0].pi = 0;
    }
 
    void add_child(long &x, long layer){
        x = a.size();
        a.push_back(Node(layer));
    }
 
    void build(string s){
        long id = 0;
        s = "#" + s;
        for(long i = 1; i<s.size(); ++i){
            visit_cnt[id]++;
            long digit = s[i] - 'a';
            long j = id;
            while(j >= 0){
                if (s[i] == s[i - a[j].layer - 1]) break;
                j = a[j].pi;
            }
 
            if (a[j].child[digit] != -1) {
                id = a[j].child[digit];
                continue;
            }
 
            add_child(a[j].child[digit], a[j].layer + 2);
            long v = a[j].child[digit];
 
            if (a[v].layer == 1) j = 1;
            else if (a[v].layer == 2) j = a[0].child[digit];
            else{
                j = a[j].pi;
                while(j >= 0){
                    if (j == 0 || (s[i] == s[i - a[j].layer - 1])) break;
                    j = a[j].pi;
                }
                j = a[j].child[digit];
            }
            a[v].pi = j;
            id = v;
        }
        visit_cnt[id]++;
    }
};
 
const int N = 3e5 + 69;
vector<int> graph[N];
int depth[N];
int sum[N];
 
void dfs(int u, int p){
    for(int v: graph[u]) if (v != p){
        dfs(v, u);
        sum[u] += sum[v];
    }
}
 
int main(void){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
 
 
    string s; cin >> s;
 
    PalindromeTree palin;
    palin.build(s);
 
    for(int i = 0; i<palin.a.size(); ++i){
        depth[i] = palin.a[i].layer;
        sum[i] = palin.visit_cnt[i];
        if (i != 0) graph[palin.a[i].pi].push_back(i);
    }
 
    dfs(0, 0);
 
    ll ans = 0;
    for(int i = 0; i<palin.a.size(); ++i) maximize(ans, 1LL *depth[i] * sum[i]);
        cout << ans << "\n";
 
    return 0;
}

Compilation message

palindrome.cpp: In member function 'void PalindromeTree::build(std::string)':
palindrome.cpp:76:26: warning: comparison of integer expressions of different signedness: 'long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   76 |         for(long i = 1; i<s.size(); ++i){
      |                         ~^~~~~~~~~
palindrome.cpp: In function 'int main()':
palindrome.cpp:131:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<PalindromeTree::Node>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  131 |     for(int i = 0; i<palin.a.size(); ++i){
      |                    ~^~~~~~~~~~~~~~~
palindrome.cpp:140:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<PalindromeTree::Node>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  140 |     for(int i = 0; i<palin.a.size(); ++i) maximize(ans, 1LL *depth[i] * sum[i]);
      |                    ~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 10840 KB Output is correct
2 Correct 2 ms 10844 KB Output is correct
3 Correct 3 ms 10844 KB Output is correct
4 Correct 3 ms 10844 KB Output is correct
5 Correct 3 ms 10844 KB Output is correct
6 Correct 3 ms 10844 KB Output is correct
7 Correct 2 ms 10844 KB Output is correct
8 Correct 3 ms 10840 KB Output is correct
9 Correct 3 ms 10844 KB Output is correct
10 Correct 2 ms 10844 KB Output is correct
11 Correct 4 ms 10712 KB Output is correct
12 Correct 3 ms 10844 KB Output is correct
13 Correct 3 ms 10844 KB Output is correct
14 Correct 2 ms 10844 KB Output is correct
15 Correct 3 ms 10844 KB Output is correct
16 Correct 3 ms 10844 KB Output is correct
17 Correct 3 ms 10844 KB Output is correct
18 Correct 3 ms 10844 KB Output is correct
19 Correct 3 ms 10844 KB Output is correct
20 Correct 3 ms 10844 KB Output is correct
21 Correct 3 ms 10704 KB Output is correct
22 Correct 3 ms 10844 KB Output is correct
23 Correct 3 ms 10844 KB Output is correct
24 Correct 2 ms 10844 KB Output is correct
25 Correct 3 ms 10844 KB Output is correct
26 Correct 3 ms 10708 KB Output is correct
27 Correct 3 ms 10840 KB Output is correct
28 Correct 3 ms 10844 KB Output is correct
29 Correct 2 ms 10844 KB Output is correct
30 Correct 3 ms 10844 KB Output is correct
31 Correct 3 ms 10844 KB Output is correct
32 Correct 3 ms 10844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 11260 KB Output is correct
2 Correct 3 ms 11004 KB Output is correct
3 Correct 3 ms 11260 KB Output is correct
4 Correct 3 ms 11004 KB Output is correct
5 Correct 3 ms 11260 KB Output is correct
6 Correct 4 ms 11260 KB Output is correct
7 Correct 3 ms 11004 KB Output is correct
8 Correct 4 ms 11260 KB Output is correct
9 Correct 4 ms 11000 KB Output is correct
10 Correct 3 ms 11096 KB Output is correct
11 Correct 3 ms 10840 KB Output is correct
12 Correct 3 ms 11000 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 15068 KB Output is correct
2 Correct 5 ms 14044 KB Output is correct
3 Correct 5 ms 14300 KB Output is correct
4 Correct 5 ms 14812 KB Output is correct
5 Correct 5 ms 13788 KB Output is correct
6 Correct 6 ms 13276 KB Output is correct
7 Correct 6 ms 14812 KB Output is correct
8 Correct 4 ms 10968 KB Output is correct
9 Correct 3 ms 11100 KB Output is correct
10 Correct 4 ms 12512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 40728 KB Output is correct
2 Correct 20 ms 39712 KB Output is correct
3 Correct 25 ms 45344 KB Output is correct
4 Correct 20 ms 41760 KB Output is correct
5 Correct 25 ms 38692 KB Output is correct
6 Correct 17 ms 38276 KB Output is correct
7 Correct 20 ms 38560 KB Output is correct
8 Correct 4 ms 11528 KB Output is correct
9 Correct 10 ms 20520 KB Output is correct
10 Correct 18 ms 38692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 82 ms 124992 KB Output is correct
2 Correct 59 ms 125768 KB Output is correct
3 Correct 68 ms 125784 KB Output is correct
4 Correct 58 ms 126804 KB Output is correct
5 Correct 69 ms 125764 KB Output is correct
6 Correct 53 ms 126020 KB Output is correct
7 Correct 41 ms 76068 KB Output is correct
8 Correct 7 ms 12180 KB Output is correct
9 Correct 7 ms 12064 KB Output is correct
10 Correct 43 ms 70580 KB Output is correct
11 Correct 67 ms 125772 KB Output is correct
12 Correct 12 ms 20804 KB Output is correct