Submission #495018

# Submission time Handle Problem Language Result Execution time Memory
495018 2021-12-17T23:53:18 Z Wolfblue Palindromes (APIO14_palindrome) C++17
100 / 100
60 ms 78360 KB
// https://oj.uz/problem/view/APIO14_palindrome
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
struct PalindromeTree {
  struct Node {
    // largest palindromic suffix
    Node *suffix;
    // add a letter to the right and left side of the palindrome
    Node *next[26];
    // length of this palindrome
    int length;
    // number of occurrences
    ll occurrences;
    ll occLazy;
  };
  Node* rootm1 = new Node();
  Node* root0 = new Node();
  vector<Node*> creationOrder;
  void init()
  {
    rootm1->length = -1;
    rootm1->suffix = rootm1;
    root0->length = 0;
    root0->suffix = rootm1;
  }
  Node *makeNode(const string &s, int i, Node *prevNode) {
    Node *newNode = new Node(); // allocate on heap
    char c = s[i];
    prevNode->next[c - 'a'] = newNode;
    newNode->length = prevNode->length + 2;
    newNode->occurrences = 0;
    newNode->occLazy = 0;
    if(newNode->length == 1) {
      newNode->suffix = root0;
    }else {
      do {
        prevNode = prevNode->suffix;
      } while (s[i - (prevNode->length + 1)] != c);
      if (!(newNode->suffix = prevNode->next[c - 'a']))
      {
        newNode->suffix = makeNode(s, i, prevNode);
      }
    }
    creationOrder.push_back(newNode);
    return newNode;
  }
  PalindromeTree(string &s) {
    init();
    Node *state = rootm1;
    s = '@' + s;
    for (int i = 1; i < s.length(); i++)
    {
      char c = s[i];
      while (s[i - (state->length + 1)] != c) {
        state = state->suffix;
      }
      Node *endState = state->next[c - 'a'];
      if (!endState) {
        endState = makeNode(s, i, state);
      }
      endState->occLazy++;
      state = endState;
    }
  }
  ll bestCount(){
    for (int i = creationOrder.size() - 1; i >= 0; i--) {
      creationOrder[i]->occurrences += creationOrder[i]->occLazy;
      creationOrder[i]->suffix->occLazy += creationOrder[i]->occLazy;
      creationOrder[i]->occLazy = 0;
    }
    ll best = 0;
    for (int i = 0; i < creationOrder.size(); i++)
    {
      best = max(best, creationOrder[i]->occurrences * creationOrder[i]->length);
    }
    return best;
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  string s;
  cin >> s;
  PalindromeTree tree(s);
  cout << tree.bestCount() << endl;
  return 0;
}

Compilation message

palindrome.cpp: In constructor 'PalindromeTree::PalindromeTree(std::string&)':
palindrome.cpp:57:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |     for (int i = 1; i < s.length(); i++)
      |                     ~~^~~~~~~~~~~~
palindrome.cpp: In member function 'll PalindromeTree::bestCount()':
palindrome.cpp:78:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<PalindromeTree::Node*>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   78 |     for (int i = 0; i < creationOrder.size(); i++)
      |                     ~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 312 KB Output is correct
2 Correct 1 ms 316 KB Output is correct
3 Correct 0 ms 208 KB Output is correct
4 Correct 0 ms 208 KB Output is correct
5 Correct 0 ms 208 KB Output is correct
6 Correct 1 ms 208 KB Output is correct
7 Correct 0 ms 208 KB Output is correct
8 Correct 0 ms 316 KB Output is correct
9 Correct 0 ms 208 KB Output is correct
10 Correct 0 ms 208 KB Output is correct
11 Correct 0 ms 208 KB Output is correct
12 Correct 0 ms 208 KB Output is correct
13 Correct 0 ms 208 KB Output is correct
14 Correct 0 ms 208 KB Output is correct
15 Correct 0 ms 208 KB Output is correct
16 Correct 0 ms 208 KB Output is correct
17 Correct 1 ms 284 KB Output is correct
18 Correct 0 ms 208 KB Output is correct
19 Correct 1 ms 320 KB Output is correct
20 Correct 0 ms 336 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 248 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 0 ms 312 KB Output is correct
25 Correct 0 ms 336 KB Output is correct
26 Correct 0 ms 336 KB Output is correct
27 Correct 0 ms 336 KB Output is correct
28 Correct 1 ms 308 KB Output is correct
29 Correct 0 ms 208 KB Output is correct
30 Correct 0 ms 316 KB Output is correct
31 Correct 0 ms 208 KB Output is correct
32 Correct 1 ms 296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 464 KB Output is correct
2 Correct 1 ms 464 KB Output is correct
3 Correct 1 ms 464 KB Output is correct
4 Correct 1 ms 464 KB Output is correct
5 Correct 1 ms 464 KB Output is correct
6 Correct 1 ms 568 KB Output is correct
7 Correct 1 ms 464 KB Output is correct
8 Correct 0 ms 464 KB Output is correct
9 Correct 1 ms 464 KB Output is correct
10 Correct 0 ms 336 KB Output is correct
11 Correct 0 ms 336 KB Output is correct
12 Correct 1 ms 444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2884 KB Output is correct
2 Correct 2 ms 2896 KB Output is correct
3 Correct 2 ms 2896 KB Output is correct
4 Correct 2 ms 2884 KB Output is correct
5 Correct 2 ms 2896 KB Output is correct
6 Correct 2 ms 2884 KB Output is correct
7 Correct 3 ms 2896 KB Output is correct
8 Correct 1 ms 324 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 2 ms 2000 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 26360 KB Output is correct
2 Correct 17 ms 26312 KB Output is correct
3 Correct 17 ms 26264 KB Output is correct
4 Correct 19 ms 26300 KB Output is correct
5 Correct 22 ms 26308 KB Output is correct
6 Correct 19 ms 19228 KB Output is correct
7 Correct 17 ms 22368 KB Output is correct
8 Correct 2 ms 716 KB Output is correct
9 Correct 5 ms 6476 KB Output is correct
10 Correct 19 ms 22512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 49 ms 78344 KB Output is correct
2 Correct 51 ms 78324 KB Output is correct
3 Correct 54 ms 78360 KB Output is correct
4 Correct 51 ms 78324 KB Output is correct
5 Correct 60 ms 78304 KB Output is correct
6 Correct 51 ms 70680 KB Output is correct
7 Correct 59 ms 65276 KB Output is correct
8 Correct 4 ms 1224 KB Output is correct
9 Correct 4 ms 1252 KB Output is correct
10 Correct 52 ms 64180 KB Output is correct
11 Correct 49 ms 78300 KB Output is correct
12 Correct 8 ms 8240 KB Output is correct