답안 #348844

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
348844 2021-01-15T22:54:33 Z ahoraSoyPeor 회문 (APIO14_palindrome) C++14
100 / 100
97 ms 46424 KB
#include <bits/stdc++.h>
using namespace std;

#define ff first
#define ss second
#define pb push_back
#define mp make_pair

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;

const int N = 500'005;
const int MOD = 1'000'000'007;
const int LOGN = 17;

//int mul ( int A, int B ) { return int((ll(A)*ll(B))%ll(MOD)); }
//int add ( int A, int B ) { return A+B >= MOD? A+B-MOD: A+B; }
//int sub ( int A, int B ) { return add ( A, MOD-B ); }

struct palindromic_tree {
  struct node{
    int len, link; ll cnt;
    map <char,int> next;
  };
  vector <node> pt;
  int last, it;
  string s;
  palindromic_tree (string str = "") {
    pt.reserve ( s.size()+2 );
    s = '#', it = 1, last = 1; //# isn't used in string
    add_node (), add_node();
    pt[1].len = -1, pt[0].link = 1;
    for ( char &c: str ) add_letter ( c );
  }
  int add_node () {
    pt.push_back({});
    return pt.size()-1;
  }
  int get_link ( int v ) {
    while ( s[it-pt[v].len-2] != s[it-1] ) v = pt[v].link;
    return v;
  }
  void add_letter ( char c ) {
    s += c, ++it;
    last = get_link ( last );
    if ( !pt[last].next.count (c) ) {
      int curr = add_node ();
      pt[curr].len = pt[last].len+2;
      pt[curr].link = pt[get_link(pt[last].link)].next[c];
      pt[last].next[c] = curr;
    }
    last = pt[last].next[c];
    pt[last].cnt++;
  }
  node& operator[](int i) { return pt[i]; }
  int size() { return pt.size(); }
};


int main () {

  ios_base::sync_with_stdio(0); cin.tie(0);

  #ifdef LOCAL
    freopen ( "in.txt", "r", stdin );
  #endif // LOCAL

  string str;
  cin >> str;

  palindromic_tree root (str);
  vector <pii> toSort (root.size()-2);
  for ( int i = 2; i < root.size(); ++i )
    toSort[i-2] = {root[i].len, i};
  sort ( toSort.rbegin(), toSort.rend() );
  ll maxi = 0;
  for ( pii it: toSort ) {
    root[root[it.ss].link].cnt += root[it.ss].cnt;
    maxi = max ( maxi, root[it.ss].cnt*it.ff );
  }
  cout << maxi << "\n";

  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 1 ms 372 KB Output is correct
5 Correct 0 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 0 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 0 ms 364 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 0 ms 364 KB Output is correct
13 Correct 0 ms 364 KB Output is correct
14 Correct 0 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 0 ms 364 KB Output is correct
17 Correct 0 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 0 ms 364 KB Output is correct
21 Correct 0 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 0 ms 364 KB Output is correct
24 Correct 0 ms 364 KB Output is correct
25 Correct 0 ms 364 KB Output is correct
26 Correct 0 ms 364 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 0 ms 364 KB Output is correct
29 Correct 0 ms 364 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
31 Correct 1 ms 364 KB Output is correct
32 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 492 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
7 Correct 1 ms 492 KB Output is correct
8 Correct 1 ms 492 KB Output is correct
9 Correct 1 ms 492 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 1740 KB Output is correct
2 Correct 3 ms 1740 KB Output is correct
3 Correct 3 ms 1740 KB Output is correct
4 Correct 3 ms 1740 KB Output is correct
5 Correct 3 ms 1740 KB Output is correct
6 Correct 3 ms 1740 KB Output is correct
7 Correct 3 ms 1740 KB Output is correct
8 Correct 1 ms 492 KB Output is correct
9 Correct 1 ms 492 KB Output is correct
10 Correct 2 ms 1228 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 12372 KB Output is correct
2 Correct 33 ms 12372 KB Output is correct
3 Correct 24 ms 12372 KB Output is correct
4 Correct 32 ms 12372 KB Output is correct
5 Correct 30 ms 12372 KB Output is correct
6 Correct 23 ms 11864 KB Output is correct
7 Correct 27 ms 11864 KB Output is correct
8 Correct 7 ms 876 KB Output is correct
9 Correct 11 ms 3428 KB Output is correct
10 Correct 24 ms 11864 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 78 ms 46400 KB Output is correct
2 Correct 97 ms 46424 KB Output is correct
3 Correct 76 ms 46416 KB Output is correct
4 Correct 92 ms 46416 KB Output is correct
5 Correct 96 ms 46416 KB Output is correct
6 Correct 85 ms 46400 KB Output is correct
7 Correct 71 ms 30248 KB Output is correct
8 Correct 21 ms 1908 KB Output is correct
9 Correct 21 ms 1908 KB Output is correct
10 Correct 71 ms 29736 KB Output is correct
11 Correct 79 ms 46416 KB Output is correct
12 Correct 25 ms 4308 KB Output is correct