Submission #530494

# Submission time Handle Problem Language Result Execution time Memory
530494 2022-02-25T15:00:42 Z Alex_tz307 Palindromes (APIO14_palindrome) C++17
100 / 100
61 ms 73700 KB
#include <bits/stdc++.h>

using namespace std;

struct node {
  node* nxt[26];
  node* failLink; /// pointer la cel mai lung sufix care este palindrom(diferit de tot palindromul)
  int len; /// lungimea
  int64_t freq; /// la final va fi frecventa palindromului in toate subsecventele

  node() {
    for (int c = 0; c < 26; ++c) {
      nxt[c] = nullptr;
    }
    failLink = nullptr;
    len = 0;
    freq = 0;
  }
};

void maxSelf(int64_t &x, int64_t y) {
  if (x < y) {
    x = y;
  }
}

struct PalindromicTree {
  node* odd;
  node* even;
  node* last; /// ultimul nod in care am ajuns pana acum
  string s;
  vector<node*> nodes; /// palindromurile obtinute in ordinea in care apar in sir

  PalindromicTree(const string &str) {
    s = str;
    odd = new node();
    nodes.emplace_back(odd);
    even = new node();
    nodes.emplace_back(even);
    odd->len = -1;
    odd->failLink = odd;
    even->failLink = odd;
    last = odd;
  }

  void extend(int pos) {
    while (s[pos - last->len - 1] != s[pos]) {
      last = last->failLink;
    }
    int ch = s[pos] - 'a';
    if (last->nxt[ch]) {
      last = last->nxt[ch];
      last->freq += 1;
      return;
    }
    last->nxt[ch] = new node();
    last->nxt[ch]->len = last->len + 2;
    node* suff = last->failLink;
    last = last->nxt[ch];
    last->freq = 1;
    last->failLink = even;
    nodes.emplace_back(last);
    if (last->len == 1) {
      return;
    }
    while (s[pos - suff->len - 1] != s[pos]) {
      suff = suff->failLink;
    }
    last->failLink = suff->nxt[ch];
  }

  int64_t solve() {
    reverse(nodes.begin(), nodes.end());
    int64_t ans = 0;
    for (auto it : nodes) {
      it->failLink->freq += it->freq;
      maxSelf(ans, it->freq * it->len);
    }
    return ans;
  }
};

void testCase() {
  string s;
  cin >> s;
  int n = s.size();
  s = '$' + s;
  PalindromicTree t(s);
  for (int i = 1; i <= n; ++i) {
    t.extend(i);
  }
  cout << t.solve() << '\n';
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int tests = 1;
  for (int tc = 0; tc < tests; ++tc) {
    testCase();
  }
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 0 ms 332 KB Output is correct
20 Correct 0 ms 332 KB Output is correct
21 Correct 0 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 0 ms 332 KB Output is correct
24 Correct 0 ms 204 KB Output is correct
25 Correct 0 ms 204 KB Output is correct
26 Correct 0 ms 332 KB Output is correct
27 Correct 0 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 0 ms 332 KB Output is correct
30 Correct 0 ms 204 KB Output is correct
31 Correct 0 ms 204 KB Output is correct
32 Correct 0 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 460 KB Output is correct
2 Correct 0 ms 460 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 460 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 0 ms 460 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 1 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2764 KB Output is correct
2 Correct 3 ms 2764 KB Output is correct
3 Correct 2 ms 2764 KB Output is correct
4 Correct 2 ms 2764 KB Output is correct
5 Correct 2 ms 2708 KB Output is correct
6 Correct 2 ms 2764 KB Output is correct
7 Correct 2 ms 2764 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 2 ms 1868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 24692 KB Output is correct
2 Correct 15 ms 24680 KB Output is correct
3 Correct 15 ms 24764 KB Output is correct
4 Correct 16 ms 24788 KB Output is correct
5 Correct 19 ms 24712 KB Output is correct
6 Correct 14 ms 18084 KB Output is correct
7 Correct 14 ms 20924 KB Output is correct
8 Correct 2 ms 712 KB Output is correct
9 Correct 4 ms 6084 KB Output is correct
10 Correct 13 ms 20980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 73700 KB Output is correct
2 Correct 56 ms 73652 KB Output is correct
3 Correct 61 ms 73616 KB Output is correct
4 Correct 48 ms 73696 KB Output is correct
5 Correct 59 ms 73584 KB Output is correct
6 Correct 43 ms 66512 KB Output is correct
7 Correct 42 ms 61400 KB Output is correct
8 Correct 4 ms 1196 KB Output is correct
9 Correct 4 ms 1196 KB Output is correct
10 Correct 43 ms 60336 KB Output is correct
11 Correct 59 ms 73644 KB Output is correct
12 Correct 8 ms 7868 KB Output is correct