답안 #530493

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
530493 2022-02-25T14:55:53 Z Alex_tz307 회문 (APIO14_palindrome) C++17
100 / 100
56 ms 74008 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();
    nodes.emplace_back(last->nxt[ch]);
    last->nxt[ch]->freq = 1;
    last->nxt[ch]->len = last->len + 2;
    last->nxt[ch]->failLink = even;
    if (last->nxt[ch]->len == 1) {
      last = last->nxt[ch];
      return;
    }
    node* suff = last->failLink;
    last = last->nxt[ch];
    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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 224 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 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 312 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 312 KB Output is correct
19 Correct 0 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 0 ms 312 KB Output is correct
22 Correct 0 ms 204 KB Output is correct
23 Correct 0 ms 332 KB Output is correct
24 Correct 0 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 0 ms 308 KB Output is correct
29 Correct 1 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 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 444 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 0 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 440 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 0 ms 460 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2752 KB Output is correct
2 Correct 2 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 2764 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 1 ms 1992 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 24764 KB Output is correct
2 Correct 16 ms 24856 KB Output is correct
3 Correct 16 ms 24856 KB Output is correct
4 Correct 16 ms 24828 KB Output is correct
5 Correct 18 ms 24828 KB Output is correct
6 Correct 14 ms 18236 KB Output is correct
7 Correct 14 ms 21048 KB Output is correct
8 Correct 2 ms 840 KB Output is correct
9 Correct 5 ms 6212 KB Output is correct
10 Correct 14 ms 21128 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 43 ms 74004 KB Output is correct
2 Correct 46 ms 73952 KB Output is correct
3 Correct 46 ms 73880 KB Output is correct
4 Correct 48 ms 73880 KB Output is correct
5 Correct 56 ms 73956 KB Output is correct
6 Correct 44 ms 66800 KB Output is correct
7 Correct 41 ms 61708 KB Output is correct
8 Correct 5 ms 1432 KB Output is correct
9 Correct 5 ms 1560 KB Output is correct
10 Correct 41 ms 60692 KB Output is correct
11 Correct 46 ms 74008 KB Output is correct
12 Correct 8 ms 8088 KB Output is correct