답안 #395494

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
395494 2021-04-28T12:06:36 Z ollel 회문 (APIO14_palindrome) C++17
100 / 100
102 ms 69360 KB
#include <bits/stdc++.h>
#include <iostream>
#include <string>
using namespace std;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef long long ll;
#define rep(i, a, b) for(int i = a; i < b; i++)

struct node {
  int n = 0, size = 0, suffix_link, next[40] = {0};
  bool f = false;
};

int MAXN = 300300;

class EerTree {
public:
  vector<node> nodes;
  vvi sufto;

  EerTree(string s) {
    nodes.resize(MAXN);
    nodes[1].size = -1; nodes[1].suffix_link = 1;
    nodes[2].size = 0; nodes[2].suffix_link = 1;

    int cur, suff = 2, n = s.size(), cursize, let, last = 3;

    rep(pos, 0, n) {
      cur = suff; let = s[pos] - 'a';

      while (true) {
        cursize = nodes[cur].size;
        if (pos - cursize - 1 >= 0 && s[pos - cursize - 1] == s[pos]) break;
        cur = nodes[cur].suffix_link;
      }

      if (nodes[cur].next[let])
      {
        cur = nodes[cur].next[let];
        nodes[cur].n++;
        suff = cur;
        // while (add > 2) {add = nodes[add].suffix_link; nodes[add].n++;}
      }
      else
      {
        nodes[last].n = 1;
        nodes[last].size = nodes[cur].size + 2;
        nodes[cur].next[let] = last;

        while (true) {
          cur = nodes[cur].suffix_link;
          cursize = nodes[cur].size;
          if (cur == 1 && nodes[cur].next[let] == last) {
            nodes[last].suffix_link = 2; break;
          }
          if (pos - cursize - 1 >= 0 && s[pos - cursize - 1] == s[pos]) {
            nodes[last].suffix_link = nodes[cur].next[let];
            break;
          }
        }

        int add = nodes[last].suffix_link;
        // while (add > 2) {nodes[add].n++; add = nodes[add].suffix_link;}
        suff = last;
        last++;
      }
    }
    queue<int> children;bool c;

    vector<bool> issuf(last, true);
    vvi sufto(last);
    rep(i, 1, last) {
      issuf[nodes[i].suffix_link] = false;
      sufto[nodes[i].suffix_link].push_back(i);
    }

    rep(i, 1, last) {
      if (issuf[i]) children.push(i);
    }

    int x;
    while (!children.empty()) {
      x = children.front(); children.pop();
      if (x <= 2 || nodes[x].f) continue;
      nodes[nodes[x].suffix_link].n += nodes[x].n;
      nodes[x].f = true;

      x = nodes[x].suffix_link;
      bool a = true;
      for (auto &nd : sufto[x]) if (!nodes[nd].f) a = false;
      if (a) children.push(x);
    }
  }
};

int main() {
  string s;cin>>s;
  EerTree et(s);
  ll best = 0;
  ll N, S;
  for (auto &nd : et.nodes) {
    N = nd.n; S = nd.size;
    best = max(best, N * S);
  }
  cout << best << endl;
}

Compilation message

palindrome.cpp: In constructor 'EerTree::EerTree(std::string)':
palindrome.cpp:63:13: warning: unused variable 'add' [-Wunused-variable]
   63 |         int add = nodes[last].suffix_link;
      |             ^~~
palindrome.cpp:69:30: warning: unused variable 'c' [-Wunused-variable]
   69 |     queue<int> children;bool c;
      |                              ^
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 51960 KB Output is correct
2 Correct 38 ms 51900 KB Output is correct
3 Correct 38 ms 51924 KB Output is correct
4 Correct 38 ms 51972 KB Output is correct
5 Correct 40 ms 51988 KB Output is correct
6 Correct 39 ms 51940 KB Output is correct
7 Correct 38 ms 51916 KB Output is correct
8 Correct 38 ms 51988 KB Output is correct
9 Correct 38 ms 52076 KB Output is correct
10 Correct 39 ms 52008 KB Output is correct
11 Correct 38 ms 51944 KB Output is correct
12 Correct 38 ms 51908 KB Output is correct
13 Correct 38 ms 51976 KB Output is correct
14 Correct 38 ms 51972 KB Output is correct
15 Correct 38 ms 52004 KB Output is correct
16 Correct 43 ms 51944 KB Output is correct
17 Correct 39 ms 51916 KB Output is correct
18 Correct 39 ms 52076 KB Output is correct
19 Correct 38 ms 51948 KB Output is correct
20 Correct 40 ms 51956 KB Output is correct
21 Correct 38 ms 51916 KB Output is correct
22 Correct 38 ms 51916 KB Output is correct
23 Correct 38 ms 51908 KB Output is correct
24 Correct 39 ms 51908 KB Output is correct
25 Correct 38 ms 51940 KB Output is correct
26 Correct 43 ms 52008 KB Output is correct
27 Correct 40 ms 52012 KB Output is correct
28 Correct 38 ms 52004 KB Output is correct
29 Correct 38 ms 52000 KB Output is correct
30 Correct 38 ms 51928 KB Output is correct
31 Correct 38 ms 51992 KB Output is correct
32 Correct 38 ms 51996 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 39 ms 51980 KB Output is correct
2 Correct 40 ms 51976 KB Output is correct
3 Correct 38 ms 52020 KB Output is correct
4 Correct 39 ms 52036 KB Output is correct
5 Correct 38 ms 52040 KB Output is correct
6 Correct 45 ms 52064 KB Output is correct
7 Correct 38 ms 52036 KB Output is correct
8 Correct 42 ms 52044 KB Output is correct
9 Correct 40 ms 51980 KB Output is correct
10 Correct 38 ms 51916 KB Output is correct
11 Correct 41 ms 51916 KB Output is correct
12 Correct 38 ms 52008 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 39 ms 52552 KB Output is correct
2 Correct 39 ms 52492 KB Output is correct
3 Correct 38 ms 52496 KB Output is correct
4 Correct 39 ms 52520 KB Output is correct
5 Correct 44 ms 52376 KB Output is correct
6 Correct 40 ms 52368 KB Output is correct
7 Correct 40 ms 52500 KB Output is correct
8 Correct 41 ms 51960 KB Output is correct
9 Correct 45 ms 51928 KB Output is correct
10 Correct 43 ms 52256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 55 ms 57704 KB Output is correct
2 Correct 54 ms 57716 KB Output is correct
3 Correct 54 ms 57700 KB Output is correct
4 Correct 56 ms 57668 KB Output is correct
5 Correct 57 ms 55972 KB Output is correct
6 Correct 51 ms 55384 KB Output is correct
7 Correct 52 ms 56540 KB Output is correct
8 Correct 42 ms 52292 KB Output is correct
9 Correct 47 ms 53472 KB Output is correct
10 Correct 55 ms 55364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 102 ms 69344 KB Output is correct
2 Correct 93 ms 69332 KB Output is correct
3 Correct 87 ms 69360 KB Output is correct
4 Correct 94 ms 69324 KB Output is correct
5 Correct 101 ms 65072 KB Output is correct
6 Correct 92 ms 66156 KB Output is correct
7 Correct 87 ms 65760 KB Output is correct
8 Correct 49 ms 52992 KB Output is correct
9 Correct 50 ms 52968 KB Output is correct
10 Correct 91 ms 62728 KB Output is correct
11 Correct 94 ms 69344 KB Output is correct
12 Correct 53 ms 54400 KB Output is correct